Even and Odd number Program in Java

In this tutorial we will learn writing the program in Java to check whether a given number is even number or not.

Before start writing Lets know

What is even and odd number?

A number is even if it is completely divisible by 2 with 0 remainder. And the number that does not completely divisible by 2 i.e. it will left some remainder known as odd number.

For example:

2, 4, 6 are even numbers because it is completely divisible by 2.

1,3,5,7 are odd numbers  because it is not completely divisible by 2.

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

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!