Repetitive Structures Java Exercise

Lesson

Flow Control

Exercise

Repetitive Structures

Objetive

Create a java program that prompts the user for two numbers and displays the numbers between them (inclusive) three times using "for", "while", and "do while" loops.

Enter the first number: 6
Enter the last number: 12

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

Example 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();
	}
}

More Java Exercises of Flow Control

Positive And Negative
Write a Java program to get a number and answer whether it is positive or negative....
Multiply If Not Zero
Write a Java program to ask the user for a number; if it is not zero, then it will ask for a second number and display their sum; otherwise, it will d...
Divide If Not Zero
Write a Java program to ask the user for two numbers and display their division if the second number is not zero; otherwise, it will display "I cannot...
Divide If Not Zero (Using Else)
Create a version of the previous program using 'else'. The program should ask the user for two numbers, and if the second number is not zero, it will ...
Greatest Of Three Numbers
Write a Java program that prompts the user to enter three numbers and displays the greatest one....
Repeat Until 0
Create a Java program to ask the user for a number "x" and display 10*x. It must repeat until the user enters 0 (using "while")....
Repeat Until 0 (Use Do While)
Create a Java program that asks the user for a number "x" and displays 10*x. The program must repeat the process until the user enters 0, using "do-wh...
While + Counter
Create a Java program to display the numbers 1 to 10 on the screen using "while"....
Multiplication Table (Use While)
Write a Java program that prompts the user to enter a number and displays its multiplication table using a 'while' loop....
Odd Numbers Descending
Create a Java program to display the odd numbers from 15 to 7 (downwards) on the screen using "while"....
Sum Numbers
Write a Java program to ask the user for an undetermined amount of numbers (until 0 is entered) and display their sum, as follows: Number? 5 Total...
Two Negative Numbers
Create a Java program to prompt the user for two numbers and determine if both numbers are negative or not....
One Or Two Negative Numbers
Create a Java program to prompt the user for two numbers and determine whether both are negative, only one is negative, or neither is negative....
Multiples
Create a Java program to display on the screen the numbers from 1 to 500 that are multiples of both 3 and 5. (Hint: Use the modulo operator to check f...
Number Repeated
Write a Java program that asks the user for a number and a quantity, and displays that number repeated as many times as the user has specified. Here's...
Password
Write a Java program to prompt the user to enter their login and password (both must be integer numbers) and repeat the prompt as many times as necess...
Password V2
Write a Java program to ask the user for their login and password (both must be integer numbers) until the entered login is "12" and the password is "...
Many Divisions
Write a Java program that asks the user for two numbers and displays their division and remainder of the division. If 0 is entered as the second numbe...
Several Multiplication Tables, (Use Do While)
Display multiplication tables from 2 to 6 using nested "do...while" loops....
Square
Write a Java program that prompts the user to enter a number and a width, and displays a square of that width, using that number for the inner symbol,...
Break & Continue
Create a Java program to write the even numbers from 10 to 20, both inclusive, except 16, in 3 different ways: Incrementing 2 in each step (use "co...
Rectangle V2
Write a Java program that asks for a number, width, and height, and displays a rectangle of that width and height, using that number for the inner sym...
Digits In A Number
Create a Java program to calculate the number of digits in a positive integer (hint: this can be done by repeatedly dividing by 10). If the user enter...
Hollow Square
Write a Java program that asks for a symbol and a width, and displays a hollow square of that width using that symbol for the outer border, as shown i...
Product
Create a Java program that asks the user for two integer numbers and shows their multiplication, but not using "*". It should use consecutive addition...
Absolute Value
Write a Java program to calculate (and display) the absolute value of a number x: if the number is positive, its absolute value is exactly the number ...
Hollow Rectangle
Write a Java program that prompts for a symbol, a width, and a height, and displays a hollow rectangle of that width and height, using that symbol for...
Statistics
Write a Java program to calculate various basic statistical operations: it will accept numbers from the user and display their sum, average, minimum a...
Switch
Create a Java program to display the text grade corresponding to a given numerical grade, using the following equivalence: 9,10 = Excellent 7,8 = ...
Conditional Operator, Positive & Smaller
Create a Java program that asks the user for two numbers and answers, using the conditional operator (?), for the following: If the first number is...
Prime Number
Write a Java program that asks the user for an integer and determines if it is a prime number or not....
Give Change
Create a Java program to give change for a purchase, using the largest possible coins (or bills). Suppose we have an unlimited amount of coins (or bil...
Exceptions
Create a Java program to prompt the user for two numbers and display their division. Errors should be caught using "try..catch"...