Java Even Odd Program

Java Even odd Program

 

In this Java program we will discuss how to find even odd number in Java.

Summary: Before writing Java even odd program, let’s read about even and odd numbers in Java.

Even numbers: Even numbers are those numbers which are be divided by 2. numbers like 4,8,22,30 etc are even numbers.

Odd Number : Odd number are those which are not divided by 2. Numbers like 3,5,11, 19 etc are odd numbers.

#Write a program to find a Even or Odd number in Java

code

import java.util.Scanner;

//find even odd number example in Java by ByteArray.in

public class JavaExamples {

public static void main(String[] args) {

// use scanner class to take input from user
Scanner sc= new Scanner(System.in);
System.out.print("Enter any number: ");
int no= sc.nextInt();

System.out.println("Entered number is :"+no);

//use modulus operator to get the remainder
if(no%2==0){
System.out.println(no+" is Even number.");
}else {
System.out.println(no+" is odd number");
}

}

}

output

Enter any number:
10
Entered number is :10
10 is Even number.

Explanation

As we read above even number are those numbers which are fully divided by 2, so to find out whether number is even or not. we use Java modulus operator.

Purpose of Java modulus operator is to get the remainder when two numbers are divided.

Use of Scanner class is to take the input from User at run time. use of nextInt() is to convert the user input to numeric number.

So, we use if modulus of number with 2 is divided by 2 and it returns 0 it means it is even number otherwise entered number is odd number in Java