Repetitive structures Learn programming Java

Lesson:

Flow Control


Exercise:

Repetitive structures 11


Objetive:

Create a java program to ask the user for two numbers and display the numbers between them (both included), three times: using "for", using "while" and using "do while"

Enter first number: 6
Enter last number 12

6 7 8 9 10 11 12
6 7 8 9 10 11 12
6 7 8 9 10 11 12


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		int number, number2;
		int i;

		System.out.print("Enter first number: ");
		number = Integer.parseInt(new Scanner(System.in).nextLine());

		System.out.print("Enter last number: ");
		number2 = Integer.parseInt(new Scanner(System.in).nextLine());

		for (i = number; i <= number2; i++)
		{
			System.out.printf(i);
		}

		System.out.println();

		i = number;
		while (i <= number2)
		{
			System.out.printf(i);
			i++;
		}
		System.out.println();


		i = number;
		do
		{
			System.out.printf(i);
			i++;
		} while (i <= number2);

		System.out.println();
	}
}