Answer to Question #250355 in Java | JSP | JSF for guru

Question #250355

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-12T07:39:53-0400
import java.util.Scanner;

public class Main {
 public static void main(String[] args) {
  Scanner scanner = new Scanner(System.in);
  int a = scanner.nextInt();
  int b = scanner.nextInt();
  int n = Math.min(a, b);
  int m = Math.max(a, b);
  for (int i = n + n % 2; i <= m; i += 2) {
   System.out.printf("%d ", i);
  }
 }
}

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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS