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.
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 ");
}
}
}
Comments
Leave a comment