Check even or odd in java without using if else, switch case and ternary operator.
Write in Java. It's very easy.
Answers
Answered by
17
Without Using any Operator
int number = 13;
String[] trick = { "even", "odd" };
System.out.println(number + " is " + trick[number % 2]);
Answered by
7
Using Bitwise AND operator
import java.util.Scanner;
public class EvenOdd {
public static void main(String[ ] args) {
System.out.print("Enter a number - ");
int number = new Scanner(System.in).nextInt( );
boolean isEven = (number & 1) != 1;
System.out.println("Is Even - " + isEven);
}
}
Sample I/O:
Case 1:
Enter a number - 12
Is Even - true
Case 2:
Enter a number - 11
Is Even - false
Similar questions