# Java program to find even or odd

Let’s learn java program to find even or odd.

## Java program to find even or odd

Java program to check whether a number is even or odd is one of the common java program interview question.

In the below even odd program in java first user enters a number as input.

Also read – garbage collection in java

This input number is divided by 2. For this we use modulus operator. If the remainder is equal to 0 then the number entered is even number else the number entered is odd number.

Now let’s see java program to check even or odd number.

```import java.util.Scanner;

public class EvenOdd
{
public static void main(String[] args)
{
int a;
System.out.println("Please enter a number to check even or odd : ");
Scanner sc = new Scanner(System.in);
a = sc.nextInt();

if(a % 2 == 0)
{
System.out.println("Entered number is an even number");
}
else
{
System.out.println("Entered number is an odd number");
}
}
}

```

Output:

Please enter a number to check even or odd : 5
Entered number is an odd number

Odd even program in java

Now let’s learn another method to check whether a number is even or odd. Similar to above java program user enters a number.

Entered number is first divided by two and then multiplied by two. If the result is same as original number then number is even number, else odd number.

For example: entered number 18, 18/2 = 9 then 9*2 =18

```import java.util.Scanner;

public class EvenOddDemo
{
public static void main(String[] args)
{
int num;
System.out.println("Please enter a number : ");
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
if((num/2)*2 == num)
{
System.out.println("Even number");
}
else
{
System.out.println("Odd number");
}
sc.close();
}
}

```

Output:

Using for loop

Now let’s learn even odd program in java using for loop. So to check if number is even or odd number, given number is divided by 2.

Using modulus operator we have to check if remainder is zero. If the remainder is zero then given number is even number else given number is odd number.

Here’s the java code to display even or odd number from 1 To n/java program to check whether a number is even or odd using for loop,

```import java.util.Scanner;

public class EvenOddDemo
{
public static void main(String[] args)
{
int a;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number(max) to print all even and odd until: ");
int number = sc.nextInt();
System.out.print("Even numbers from 1 to " + number + " are: \n");
for(a = 1; a <= number; a++)
{
if(a%2 == 0)
{
System.out.print(a + "\n");
}
}
System.out.print("Odd numbers from 1 to " + number + " are: \n");
for(a = 1; a <= number; a++)
{
if(a%2 == 1)
{
System.out.print(a + "\n");
}
}
sc.close();
}
}

```

Output:

Enter number(max) to print all even and odd until: 10
Even numbers from 1 to 10 are:
2
4
6
8
10
Odd numbers from 1 to 10 are:
1
3
5
7
9