Written by Sai Sandeep Thota on June 30, 2013 in Java > Programming

Java Program to find Number is Even or Odd

GET ALERTS:

Get our Latest updates delivered to your mailbox!

This is a java program used to find given number is even or odd. We use the modulo operator to check whether the given number is even or odd. If the remainder is 1 then the given number is odd, if the remainder is 0 then the number is even.

Java Program to find Number is Even or Odd

import java.util.Scanner;
class EvenOdd {
	public static void main(String args[])
	{
		int n;
		System.out.println("Enter a Number:");
		Scanner get = new Scanner(System.in);
		n = get.nextInt();
		if(n % 2 == 0)
		{
			System.out.println("Given Number "+n+" is Even");
		}
		else
		{
			System.out.println("Given Number "+n+" is Odd");
		}
	}
}

Java Program to find Number is Even or Odd using Classes

This is the exact replica of the above program but here we are using a separate class for obtaining the input from the user and creating an object for the Input class from the main using new.

import java.util.Scanner;

class GetNum {
	void get()
	{
		int n;
		System.out.println("Enter a Number:");
		Scanner get = new Scanner(System.in);
		n = get.nextInt();
		if(n % 2 == 0)
		{
			System.out.println("Given Number "+n+" is Even");
		}
		else
		{
			System.out.println("Given Number "+n+" is Odd");
		}
	}
}

class EvenOdd_Class {
	public static void main(String args[])
	{
			GetNum myObj = new GetNum();
			myObj.get();
	}
}

Output:
Java Program to find Number is Even or Odd

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: