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
Other Posts You May Like
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
- Java Program to Calculate the Average of an Array
- Java Program to Calculate the Power of a Number
- Java Program to Find the Factorial of a Number using Loop
- Java Program to Generate Random Numbers
- Java Program to Check if Given Year is Leap or Not
Comments
Post a Comment