Even Odd number program in Java

In this section you will learn how to find whether a number is even or odd in Java. We will use if else condition and small mathematic operation to check the even or odd number and we will show the result on output screen. In this program we are taking the input from user.

In this program we are taking the input from the user. We are using scanner function to take the input from user. To take the input from user we will import scanner package in our program.

What is scanner function?

Scanner function is use to take the input from user in java.

Example of even number:

2, 4, 6, 8, 10, 12, etc.

Example of odd number:

1, 3, 5, 7, 8, 9, 11, etc.

How program will work?

Program of finding even odd number in Java


	import java.util.Scanner;

	class EvenOdd
	{
		public static void main(String arg[])
		{
			int n;
			Scanner sc = new Scanner(System.in);
			System.out.println("ENTER A NUMBER:-");
			n = sc.nextInt();
			if(n%2 == 0)
			{
				System.out.println("ENTER NUMBER IS EVEN.");
			}
			else
			{
				System.out.println("ENTER NUMBER IS ODD.");
			}
		}
	}

Output:


	ENTER A NUMBER:- 5
	ENTER NUMBER IS ODD.

Advertisment

Post Your Comment