🧠Introduction
In Java programming, one of the basic concepts every beginner learns is determining whether a number is odd or even.
This small yet useful program helps build logic using conditional statements and the modulus operator (%).
💻 Java Program: Check Odd or Even Number
import java.util.Scanner;
public class OddEvenCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Taking input from user
System.out.print("Enter a number: ");
int number = sc.nextInt();
// Checking if number is even or odd
if (number % 2 == 0) {
System.out.println(number + " is an Even number.");
} else {
System.out.println(number + " is an Odd number.");
}
sc.close();
}
}
⚙️ Explanation:
-
Scannerclass – Used to take user input from the keyboard. -
number % 2 == 0– The modulus operator finds the remainder.-
If the remainder is 0, the number is even.
-
Otherwise, it’s odd.
-
-
Output is displayed using
System.out.println().
🧾 Example Output:
Enter a number: 7
7 is an Odd number.
Enter a number: 12
12 is an Even number.
🧩 Conclusion
This program is a simple and effective way to understand conditional statements and operators in Java.
It’s a great starting point for beginners who are learning how to take input and apply logic in Java.
Comments
Post a Comment