Answer to Question #227953 in Java | JSP | JSF for jeffy

Question #227953

Write a program that uses while loops and perform the following steps:

1) Prompt the user to input two integers: start and end (start must be less than end).

2) Output the average of the even numbers between start and end.


1
Expert's answer
2021-08-20T12:58:37-0400
import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
	    Scanner input = new Scanner(System.in);
	    	System.out.println("Enter the start integer");
	    int start = input.nextInt();
	    System.out.println("Enter the end integer");
	    int end = input.nextInt(); 
		if(end>start){
		    int i = start;
		    int count =0;
		    double sum =0.0;
		    while(i<=end){
		        if(i%2==0){
		            sum += i;
		            count ++;
		        }
		        i++;
		    }
		    System.out.println("The average of even numbers is  "+sum/count);
		}
		else{
		    System.out.println("Invalid start and end integers  ");
		}
	}
}

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