//Qus:- #4. Write a Java Program Without Using 'if ' Check User Entered Number is Even or odd.
- Check User Entered Number is Even Or odd without ' If ' Statement?
Solution:-
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n = sc.nextInt();
String number=(n%2==0)?"Even":"Odd"; //Using Ternary operator ( ? : )
System.out.println(n+" is "+ number);
}
}
🤖Explanation: let's Suppose User Entered Number is (n=21).
Example 1:- Check User Entered number is even or Odd just applying condition (n%2==0)
if the user entered number is divisible by 2 then it's print Even otherwise prints Odd.
👉 21%2= 1(Reminder)
🤙this number is not satisfied with the condition so it's Print Odd.
Example 2:- let's Suppose User Entered Number is (n=20);
let's check the condition:- (n%2==0)
👉 20%2=0 (Reminder)
🤙User entered number is satisfied the Condition so it's print Even
🤦♂️Try Your Self Buddy, If you Get any Error just Comment Below, Definitely, I will try to Solve Your Problem.
Suggestion:- 500+ Java Programs List With Examples and Output.
Admin Words:- I am always tried to solve this program in a simple way. Which helps you to understand easily? Hopefully, now you can understand How to check Even Or odd without ' If ' Statement. If you find any error in this program please remind us to comment below of this post. And stay away to learn programs J with Us. and also Read our Next Program.
Next Program:- Write a Java Program To Check User Enter Number is Even Or Not Without Modulus Operator?
Recommended: -
0 Comments