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

 

Similar post

C program to find a number is even or odd using the function

C program to separate Odd and Even numbers from an array

C program to Electricity bill calculation using the function

C program to display all even and odd numbers from 1 to n

C program display odd and even numbers without if statements

C program to calculate the sum of odd and even numbers

C program to find whether a number is even or odd

 

Java program to find a number is even or odd using the method

Java program to separate Odd and Even numbers from an array

Java program to display all even and odd numbers from 1 to n

Java program display odd and even numbers without if statements

Java program to calculate the sum of odd and even numbers

Java program to find whether a number is even or odd

 

 

 

 

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

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago