Check ODD and EVEN
Table of Contents
In this tutorial, we will discuss the Java program to check whether the number is even or odd
In this post, we are going to learn how to check whether the given numbers is even or odd using different 5 ways in Java
Program 1
class CheckEvenOdd{
public static void main (String args[]){
int num1=57,num2=46;
if(num1%2==0)
System.out.print(num1+" is a even number");
else
System.out.print(num1+" is a odd number");
System.out.print("\n");
if(num2%2==0)
System.out.print(num2+" is a even number");
else
System.out.print(num2+" is a odd number");
}
} When the above code is executed, it produces the following result
57 is a odd number 46 is a even numbers
In this program,
Program 2
import java.util.Scanner;
class CheckEvenOdd1{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);//Scanner class
System.out.print("Enter a number: ");
int num=scan.nextInt();//get input from user
if(num%2==0)
System.out.print(num+" is a even number");
else
System.out.print(num+" is a odd number");
}
} When the above code is executed, it produces the following result
case 1
Enter a number: 45 45 is a odd number
case 2
Enter a number: 34 34 is a even number
Approach
Program 3
import java.util.Scanner;
class CheckOddEvenSwitch3{
public static void main(String args[]){
Scanner scan=new Scanner(System.in); //create a scanner object for input
System.out.print("Enter the integer number: ");
int num=scan.nextInt();//get input from the user for num
switch(num%2){
case 0:
System.out.println(num+" is an Even number");
break;
case 1:
System.out.println(num+" is an Odd number");
}
}
} When the above code is executed, it produces the following result
case 1
Enter the integer number: 154 154 is an even number
case 2
Enter the integer number: 113 113 is an odd number
Approach
Program 4
import java.util.Scanner;
class CheckOddEvenMethod{
public static void main(String args[]){
Scanner scan=new Scanner(System.in); //create a scanner object for input
System.out.print("Enter the integer number: ");
int num=scan.nextInt();//get input from the user for num
checkEvenodd(num);//call the method
}
//user defined method for check odd even
public static void checkEvenodd(int num) {
if(num%2==0){
System.out.print(num+" is an even number ");
}
else{
System.out.print(num+" is an odd number ");
}
}
} When the above code is executed, it produces the following result
case 1
Enter the integer number: 234 234 is a even number
case 2
Enter the integer number: 765 765 is a odd number
In the program,
Program 5
import java.util.Scanner;
class CheckOddEvenUsingObject{
Scanner scan;
int num;
void getValue(){//user defined method for get vaue from user
scan=new Scanner(System.in); //create a scanner object for input
System.out.print("Enter the integer number: ");
num=scan.nextInt();//get input from the user for num
}
//user defined method for check odd even
void checkEvenodd() {
if(num%2==0){
System.out.print(num+" is an even number ");
}
else{
System.out.print(num+" is an odd number ");
}
}
}
class MainClass{//main class
public static void main(String args[]){
CheckOddEvenUsingObject obj=new CheckOddEvenUsingObject();
obj.getValue();
obj.checkEvenodd();//calling the method
}
}
When the above code is executed, it produces the following result
case 1
Enter the integer number 1234 1234 is even
case 2
Enter the integer number 12345 12345 is odd
Similar post
Python program to check whether a number is even or odd
C program to check whether a number is even or odd
C++ program to check whether a number is even or odd
Suggested post
Data type and variable in Java language
variable types in Java language
Recursion in Java language
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.