# Even and Odd Number Program in Java

In this tutorial, we are going to learn writing java program to check a given number is even or odd. For this purpose we will be using if else statement of java concept.

## Problem Statement

For any number that is input by the user, we have to check if it is even or odd.

Case1: If the user inputs number 51
then the output should be ‘odd’.
Case2: If the user inputs a number 100.
then the output should be ‘even’.

## Our Logic to find Even and Odd Number

• Our program will take a number as input from the user.
• The input number is checked if it is an even number or not.
• If it is, then print ‘even,’ and if it is not, the result should print ‘odd.’

## Let’s discuss how to check if a number is an even number.

An integer when divided by ‘2’ and leaves the remainder 0 is called an even number. We can also define an even number as a number that ends with either 0 or 2 or 4 or 6 or 8 is considered an even number.

All the numbers other than even number is considered odd number. Odd numbers always leave the remainder ‘1’ when divided by ‘2’.

Our program divides the input number by ‘2’. If the remainder is ‘0’, we consider a number an even number; otherwise, it is an odd number.

## Algorithm to check number is even or odd

Step 1: Start

Step 2: Declare an variable num of integer type.

Step 3: Take an integer input from the user and store it into variable num.

Step 4: if num is divisible by 2

Print ‘even’
else
Print ‘odd’

Step 5: Stop

## Program 1: Java program for even and odd

``````import java.util.*;
public class Main
{
public static void main(String[] args) {
int num;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to check even or odd ");
num= sc.nextInt();
if(num%2==0){
System.out.println("Given number "+num+" is even");
}else{
System.out.println("Given number "+ num +" is odd");
}
}
}``````

Output

``````Enter a number to check even or odd
56
Given number 56 is even``````

Explanation

• The input integer is 22, which is then checked if it’s divisible by 2.
• 5%2 = 0, which is divisible by 2, this makes the integer 22 an even integer.

## Program 2: Even and odd  program in java using methods

``````import java.util.*;
public class Main
{
static void oddEven(int num) {
if(num%2==0){
System.out.println("Given number "+num+" is even");
}else{
System.out.println("Given number "+num+" is odd");
}
}
public static void main(String[] args) {
int num;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to check even or odd ");
num= sc.nextInt();
oddEven(num);
}
}``````

Output

``````Enter a number to check even or odd
54
Given number 54 is even``````

## Program 3: Java 8 Program to check even and odd

``````import java.util.*;
import java.util.stream.*;
public class Main
{
public static void main(String[] args) {
int num;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to check even or odd ");
num= sc.nextInt();
String result = isEven(num) ? "Even" : "Odd";
System.out.println(num + " is " + result);
}
private static boolean isEven(int number) {
return IntStream.of(number).anyMatch(n -> n % 2 == 0);
}
}``````

Output

``````Enter a number to check even or odd
33
33 is Odd``````

## Conclusion

In this tutorial we have learnt writing java programs to check whether a given number is even or odd. The concepts of even and odd numbers were briefly explained in this tutorial. An even number is divisible by 2 with no remainder, while an odd number leaves a remainder when divided by 2.

We have seen three different approach to write even odd program:

1. Basic Program for Even and Odd: This program used a straightforward approach to check whether the input number is even or odd using the modulo operator.
2. Program using Methods: The second program introduced a method called `oddEven` to encapsulate the logic for checking even or odd. This modular approach makes the code more readable and reusable.
3. Java 8 Program: The third program leveraged Java 8 streams and the `anyMatch` method to check if a number is even. This showcased a more modern and concise way of achieving the same result.

Throughout the examples, we are taking user input and determined whether it was even or odd by applying our even odd logic. We have written multiple approaches to implement even and odd number checks in Java, catering to different coding preferences and styles.

Happy coding!