Find elements

Java code to check a number is even or odd using Method

Java code to check a number is even or odd using Method

In this program, we will discuss the Java code to check a number is even or odd using Method

In this program, we are going to learn about how to find  odd or even number from given number using the method in the Java language

 

 

What is Even or Odd

When the number is divided by 2 and the balance becomes zero and the above number is called as the even number – eg 2,4,6,8,10

and on the other sides when it is divided by 2 and balance becomes 1 they are called odd numbers or uneven numbers

 

In my previous post, I have explained the various ways to check whether the  number is even or odd in Java language

However, in this program, we embed the logic of the check even or odd number program in the method

you can embed the logic of the program to check even or odd numbers using any of the following approaches in the method

 

Once This program received the number it will check the given number either odd or even.

After receiving the input from the user, it is stored in the variable of num and then program divides the value of num by 2 and displays the output

Program 1

Using modular operator

import java.util.Scanner;
class Even_Odd1{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the number for check odd or even: ");

int num=scan.nextInt();
//reads the value from the user
find_Oddeven(num);//calling the method
}

//create a user defined metod
static void find_Oddeven(int num){//method definition
if(num%2==0) 
    System.out.println(num+" is even"); 
else 
    System.out.println(num+" is odd");
}
}

 

When the above code is executed, it produces the following results

Case 1

Enter a number for check odd or even: 234
234 Is even

Case 2

Enter a number for check odd or even: 253
253 is odd

Using modular operator with the return

Program 2

import java.util.Scanner;
class Even_Odd1{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the number for check odd or even: ");

int num=scan.nextInt();
if(num%2==0) 
    System.out.println(num+" is even"); 
else 
    System.out.println(num+" is odd");
//reads the value from the user
find_Oddeven(num);//calling the method
}

//create a user defined method with return
static boolean find_Oddeven(int num){//method definition
return (num%2==0);//return the value
}
}

When the above code is executed, it produces the following results

Case 1

Enter a number to check odd or even
344

344 is  even

 

Case 2

Enter a number to check odd or even
47
47 is odd

 

Program 3

Using Bitwise operator

import java.util.Scanner;
class OddEvenMethod5{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
System.out.print("Enter the number for check odd or even: ");
int num=scan.nextInt();//reads the value from the user
isEvenOrOdd(num); //call the method 

}

//create a user define method to find odd or even
static void isEvenOrOdd(int num){//method for find even or odd
if((num & 1) ==1) 
    System.out.print(num+ "is an odd number"); 
else 
    System.out.print(num+ "is an even number"); 
}
}

When the above code is executed, it produces the following results

Case 1

Enter a number for check odd or even: 654
654 Is an even number

Case 2

Enter a number for check odd or even: 323
323 is an odd number

 

Program 4

Using ternary operator

import java.util.Scanner;
class OddEvenMethod4{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
System.out.print("Enter the number for check odd or even: ");
int num=scan.nextInt();//reads the value from the user
isEvenOrOdd(num); //call the method 

}

//create a user defined method to fine odd or even
static void isEvenOrOdd(int num){//method for find even or odd
String oddEven=(num % 2 == 0)? "Even":"Odd";
System.out.println(num+" is "+oddEven);
}
}

 

When the above code is executed, it produces the following results

Case 1

Enter a number for check odd or even: 567
567 Is odd

Case 2

Enter a number for check odd or even: 124
124 is even

Program 5

Using Division operator

import java.util.Scanner;
class OddEvenMethod2{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
System.out.print("Enter the number for check odd or even: ");
int num=scan.nextInt();//reads the value from the user
isEvenOrOdd(num); //call the method 

}

//create a user defined method to find odd even
static void isEvenOrOdd(int num){//method for find even or odd
if((num/2)*2==num)
    System.out.println(num+" is even");
else
    System.out.println(num+" is odd");
}
}

When the above code is executed, it produces the following results

Case 1

Enter the number for check odd or even: 501
501 is Odd

Case 2

Enter the number for check odd or even: 200
200 is even

 

Program 6

Using shift operator

import java.util.Scanner;
class OddEvenMethod1{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
System.out.print("Enter the number for check odd or even: ");
int num=scan.nextInt();//get input from user
isEvenOrOdd(num); //call the method 

}

static void isEvenOrOdd(int num){//methods for find even or odd
if(((num >> 1)<<1) ==num)
    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 results

Case 1

Enter the number for check odd or even: 501
501 is a Odd number

Case 2

Enter the number for check odd or even: 200
200 is a Even number

 

Suggested for you

Operator in Java

Method in Java

Data types and variable in Java

If statements in Java

C function to check a number is even or odd
Cpp program to display all even or odd numbers from 1 to n
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago