In: Computer Science
Write an application that asks a user to type an even number or the sentinel value 999 to stop. When the user types an even number, display the message “Good job!” and then ask for another input. When the user types an odd number, display an error message, "x is not an even number", and then ask for another input. When the user types the sentinel value 999, end the program.
In the code, firstly user will ask to enter the number
Then, we have used while loop till the enetered number becomes 999
If number is not 999 , then control will move inside the loop and check whether if it is even or odd
If the number is even , which is checked with statement num%2==0 , control will move inside the If statement and print "Good Job" in console
If number is odd, control will move inside Else statement and print "x is not an even number" in console
And then again , ask user to enter the number , if enetered number is 999 , control will exit the loop and terminate the program
If number entered is not 999 , again control will move inside the loop
Please find the below code in JAVA:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
System.out.println("Enter even number or 999 sentinel");
int number=scr.nextInt();
while (number!=999){
if(number%2==0){
System.out.println("Good Job");
}
else
{
System.out.println("x is not a even number");
}
System.out.println("Enter even number or 999 sentinel");
number=scr.nextInt();
}
}
}
Please find the output screnshot: