java

Java program to check whether the number is even or odd|5 ways

Java program to check whether the number is even or odd

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

Check whether the number is even or odd

Check a number is Even or Odd -stranded method

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,

  • integer variables num1 and num2 is declared and initialized
  • The given numbers are tested whether it is odd or even
  • Then,the program is displayed the output of using System.out.print() function.

 

Check a number is Even or Odd – Entered by user

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

  • integer variable num1 is declared.
  • The program asks input from the user
  • Then the user enters the input values for num1.
  • The program will read the input using Scanner class and store to the variable num1
  • The given number is tested whether it is odd or even using if statements..
  • Then,the program is displayed the output of using System.out.print() function,

 

Check a number is Even or Odd – Switch statements

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

  • integer variable num is declared.
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using Scanner class and store to the variable num.
  • The given number is tested whether it is odd or even using switch statements..
  • Then,the program is displayed the output of using System.out.print() function,

Check a number is Even or Odd – using method

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,

  • Declare variables num.
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using scanner class and store the variables num1.
  • Define the method(quotientNum()) to check whether the given number odd or even
  • Call the method to produce output
  • finally, the program displays output using System.out.println()

 

Check a number is Even or Odd – using object

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

Operator in Java language

if statement in Java language

For loop in Java language

Method in Java language

Recursion in Java language

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.