JAVA Write a Java program (called practice_6_3) that uses a while loop to determ
ID: 640937 • Letter: J
Question
JAVA Write a Java program (called practice_6_3) that uses a while loop to determine and print out all even numbers between 50 and 100 on a single line, separated by commas. Then another while loop in the same program to print out all odd numbers between 50 and 100 on a new line, separated by commas. Use proper label for the outputs as shown in the example below. Even numbers between 50 and 100: 50, 52, 54, 56, 58, 60, 62, 64, ... Odd numbers between 50 and 100: 51, 53, 55, 57, 59, 61, 63, 65, ...Explanation / Answer
public class Practice_6_3 {
public static void main(String[] args) {
System.out.print("Even numbers between 50 and 100: " );
int i=50;
while(i <= 100){
// if the number is divisible by 2 then it is even
if( i % 2 == 0){
System.out.print(i);
if ( i < 100)
System.out.print(", ");
}
i++;
}
System.out.println("");
i=50;
System.out.print("Odd numbers between 50 and 100: " );
while(i <= 100){
// if the number is divisible by 2 then it is even
if( i % 2 == 1){
System.out.print(i);
if (i<99)
System.out.print(", ");
}
i++;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.