Create a for loop program that prompts a user for two integers. If the second integer is greater than the first one, your program should then display multiples of the first integer up till the product of the two entered values. If the second integer is smaller than the first, your program displays error.
Sample Run 1 Enter integer1: 2 Enter integer2: 5 Output 1: multiples = 2 4 6 8 10
import java.util.Scanner;
public class App {
/**
* The start point of the program
*
* @param args
*/
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
// prompts a user for two integers.
System.out.print("Enter integer1: ");
int integer1 = keyBoard.nextInt();
System.out.print("Enter integer2: ");
int integer2 = keyBoard.nextInt();
// If the second integer is greater than the first one, your program should then
// display multiples of the first integer up till the product of the two entered
// values. If the second integer is smaller than the first, your program
// displays error.
if (integer2 > integer1) {
System.out.print("multiples = ");
for (int i = integer1; i <= integer2; i++) {
System.out.print((i * 2) + " ");
}
} else {
System.out.println("Error: the second integer is smaller than the first.");
}
keyBoard.close();
}
}
Comments
Leave a comment