Java Program to Check if a Given Number is Odd or Even - The Coding Shala

Home >> Java Programs >> Check if Number is Odd or Even

 In this post, we will learn how to Check if a Given Number is Odd or Even in Java.

Java Program to Check if a Given Number is Odd or Even

Take a number as user input and check the number whether it is odd or even using Java Program.

Example 1
Input: 5
Output: Odd

Example 2
Input: 4
Output: Even

Approach 1

If a number is perfectly divided by 2 then it's even else it's odd.

Java Program: 

// Java program to Check even or odd number

import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) { 
		
		Scanner sc = new Scanner(System.in);
		
		System.out.println("Enter a Number");
		int num = sc.nextInt();
		
		if (num % 2 == 0) {
			System.out.println(num + " is an even number");
		} else {
			System.out.println(num + " is an odd number");
		}
		
		sc.close();
	}
}

Output:  

Enter a Number
4
4 is an even number


Other Posts You May Like
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Add two numbers in Scala - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Goal Parser Interpretation LeetCode Solution - The Coding Shala

Maximum Population Year LeetCode Solution - The Coding Shala

Shell Script to find sum, product and average of given numbers - The Coding Shala