Java even odd program
How to write a java program that will determine whether a number is even or odd. I am writing the code in my mobile.
Source code for java even and odd
package asicQuestion;
public class evenOdd {
public static void main(String[] args){
System.out.println("Enter the number to check even or odd");
java.util.Scanner sc = new java.util.Scanner(System.in);
int num = sc.nextInt();
if(num == 0){
System.out.println("The number is zero");
}
else if(num % 2 == 0 && num != 0){
System.out.println("The number is even");
}
else{
System.out.println("THE number is odd");
}
}
}
Output:
Java even odd program using conditional ternary operator :-
public class evenOddUsingTernary {
public static void main(String[] args){
int num = 11;
System.out.println(num%2==0?"Even number":"Odd number");
}
}
- The conditional ternary operator ( ? :), will work like if-else statement.
(Condition) ? "It will execute, if condition true" : "It will execute, if condition false";
Logic for even odd in java :-
- When we divide a number with 2 and if the remainder will be 0, then the number will be even.
- Otherwise the number will be odd number.
Please check these topics :-
- Find quotient and remainder in java :- click here
- Java scanner :- click here