Answer to Question #252694 in Java | JSP | JSF for Silas Petrus

Question #252694
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
1
Expert's answer
2021-10-18T00:14:34-0400

Source code

import java.util.Scanner; 
public class Main
{
	public static void main(String[] args) {
	    Scanner in=new Scanner(System.in);
	    
	    System.out.println("Enter the starting number: ");
	    int start=in.nextInt();
	    
	    System.out.println("Enter the stoping number: ");
	    int stop=in.nextInt();
	    
	    if(start>stop){
	        int temp=start;
	        start=stop;
	        stop=temp;
	    }
	    
	    System.out.println("Even numbers between "+start+" and "+stop+" are: ");
	    for(int i=start;i<=stop;i++){
	        if(i%2==0){
	            System.out.print(i+" ");
	        }
	    }
	    
	}
}


Sample run





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