write a java program to find even and odd number?

In this java program we will check whether a number is odd or even by taking input from commandline or console.

1) When a number is divided by 2 and if we get remainder as 0 we say it is an even number.

2) When a number is divided by  2 and if we remainder is not 0 then we say it is odd number.

To get remainder we use modulo or percentile operator.

Input :

Enter Number: 2

Output : 2 is even

Enter Number :1

Output : 1 is odd

Ex : 4%2=0

Java Program to find even and odd number:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class EvenOddDemo {

	public static void main(String[] args) throws NumberFormatException, IOException {

		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter the number");
		Integer number=Integer.parseInt(br.readLine());
		if(number%2==0){
			System.out.println(number+"=is even");
		}else
		{
			System.out.println(number+"=is odd");
		}
	}

}

Output :

Enter the number
5
5=is odd

Enter the number
2
2=is even

Leave a Reply

Your email address will not be published. Required fields are marked *