Write a Java program to ask the user to input a number from the keyboard. The ou
ID: 3833700 • Letter: W
Question
Write a Java program to ask the user to input a number from the keyboard. The output should indicate whether it is positive or negative, a floating number or an integer, an even number or an odd number (Use IF...ELSE statements). The execution of the program should be (Case 1) Please input a number: 35.6 35.6 is a positive, floating number. (Case 2) Please input a number: -28 -28 is a negative, even number. Write a program called CheckPassFail which prints "PASS" if the int variable "mark" is more than or equal to 50; or prints "FAIL" otherwise. The program shall always print "DONE" before exiting (Use IF...ELSE statement).Explanation / Answer
Please find my implementation.
1)
import java.util.Scanner;
public class CheckInput {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a number: ");
float num = keyboard.nextFloat();
if(num >= 0){
int x = (int)num;
if(x == num){
System.out.print(x+" is a positive, ");
if(x%2 == 0){
System.out.println("even number.");
}else{
System.out.println("odd number.");
}
}else{
System.out.print(num+" is a positive, floating number");
}
}else{
int x = (int)num;
if(x == num){
System.out.print(x+" is a negative, ");
if(x%2 == 0){
System.out.println("even number.");
}else{
System.out.println("odd number.");
}
}else{
System.out.print(num+" is a negative, floating number");
}
}
keyboard.close();
}
}
/*
Sample run:
Enter a number: 35.6
35.6 is a positive, floating number
Enter a number: -28
-28 is a negative, even number.
*/
2)
import java.util.Scanner;
public class CheckPassFail {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter mark: ");
int mark = keyboard.nextInt();
if(mark >= 50){
System.out.println("PASS");
}else{
System.out.println("FAIL");
}
System.out.println("DONE");
keyboard.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.