Answer to Question #250619 in Java | JSP | JSF for Amadix

Question #250619

Using a for loop create a program that will prompts the user for two numbers and then print out a list of even number in between the two given numbers(inclusive), starting from the small number to the big number

Sample Run1

Enter two numbers: 1 10

Output1: 2 4 6 8 10

Sample Run2

Enter two numbers: 9 5

Output2: 6 8


1
Expert's answer
2021-10-13T02:00:09-0400
import java.util.Scanner;


public class App {


	public static void main(String[] args) {
		Scanner keyBoard = new Scanner(System.in);
		System.out.print("Enter two numbers: ");
		int number1 = keyBoard.nextInt();
		int number2 = keyBoard.nextInt();
		int temp;
		if (number1 > number2) {
			temp = number2;
			number2 = number1;
			number1 = temp;
		}
		for (int i = number1; i <= number2; i++) {
			if (i % 2 == 0) {
				System.out.print(i + " ");
			}
		}
		keyBoard.close();
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Amadhila Tobias
13.10.21, 23:44

Thanks a lot it's working

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS