A java application called Numbers, that reads two integers from the keyboard (a
ID: 3675186 • Letter: A
Question
A java application called Numbers, that reads two integers from the keyboard (a minimum and a maximum range).
Based on the minimum and the maximum integers, the application should:
List all of the numbers in the range
List the even numbers in the range (using a conditional operator)
List the odd numbers in the range (using a conditional operator)
List the total numbers in the range
List the sum of all the numbers in the range
Calculate the average of the numbers in the range
Using at least one for loop, either a while loop or a do while loop and a conditional operator for calculating even and odd numbers.
So the output should look like this;
Enter the minimum value of the range of numbers: 3
Enter the maximum value of the range of numbers: 6
The numbers in the range are : 3 4 5 6
The even numbers in the range are : 4 6
The odd numbers in the range are : 3 5
The total numbers in the range are : 4
The sum of the integers is : 18
The average number in the range is : 4
Explanation / Answer
import java.util.Scanner;
public class Numbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m,n;
//reading min and max
System.out.print("Enter the minimum value of the range of numbers: ");
m = sc.nextInt();
System.out.print("Enter the maximum value of the range of numbers: ");
n = sc.nextInt();
//printing all integers in given range
System.out.print("The numbers in the range are: ");
for(int i=m; i<=n; i++){
System.out.print(m+" ");
}
System.out.println();
//printing even integers in given range
System.out.print("The even numbers in the range are : ");
for(int i=m; i<=n; i++){
//using conditional operator
boolean isEven = (i%2==0) ? true : false;
if(isEven)
System.out.print(i+" ");
}
System.out.println();
//printing odd integers in given range
System.out.print("The odd numbers in the range are : ");
for(int i=m; i<=n; i++){
//using conditional operator
boolean isOdd = (i%2==0) ? false : true;
if(isOdd)
System.out.print(i+" ");
}
System.out.println();
System.out.println("The total numbers in the range are: "+(n-m+1));
int sum=0;
for(int i=m; i<=n; i++)
sum+=i;
System.out.println("The sum in the integers is: "+sum);
System.out.println("The average number in the range is: "+(sum/(n-m+1)));
}
}
/*
Output:
Enter the minimum value of the range of numbers: 3
Enter the maximum value of the range of numbers: 6
The numbers in the range are: 3 3 3 3
The even numbers in the range are : 4 6
The odd numbers in the range are : 3 5
The total numbers in the range are: 4
The sum in the integers is: 18
The average number in the range is: 4
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.