Java program to find given number is even or odd

Java program to find given number is even or odd

Method 1:-
Java program to find given number is even or odd using conditional operator

Program:-

import java.util.Scanner;

class group{
public static void main(String arg[]){
int num;
//Read a number
Scanner data = new Scanner(System.in);
System.out.println("Enter a number");
num = data.nextInt();
// Conditional operator
System.out.println((num%2)==0 ? "even number":"odd number");
}
}

Output 1:-

Enter a number
17
odd number


Output 2

Enter a number
58
even number


Output 3

Enter a number
-25
odd number


Output 4

Enter a number
-24
even number



Method 2:-
Java program to find given number is even or odd using if else statement

Program:-

import java.util.Scanner;

class group{
public static void main(String arg[]){
int num;
Scanner data = new Scanner(System.in);
//Read a number
System.out.println("Enter a number");
num = data.nextInt();
// if-else statement
if((num%2)==0)
System.out.println("even number");
else
System.out.println("odd number");
}
}


Output 1:-

Enter a number
59
odd number


Output 2

Enter a number
98
even number


Output 3
Enter a number
-56
even number


Output 4

Enter a number
-23
odd number




Learn Java:-
Java codes

Java Programs:-
List of Java Programs

3 Comments

Add your comments here

Back to Top