In the world of programming, certain tasks serve as the foundational building blocks for learning new concepts. For aspiring Java programmers, creating a simple program to determine if a number is odd or even is one such task. Not only does this exercise familiarize them with the language’s syntax and structure, but it also introduces the importance of conditionals in programming logic. This article offers a detailed look at creating a Java program to check if a number is odd or even.
Before we proceed, let’s remember a fundamental arithmetic principle: any number that is perfectly divisible by 2 is an even number. If a number is not perfectly divisible by 2 (i.e., there’s a remainder), it is an odd number.
Writing the Java Program
Now, let’s get to the crux of the matter—writing the Java program to check if a number is odd or even. The program logic is straightforward: if a number divided by 2 leaves a remainder of 0, it is even; otherwise, it is odd.
Start by creating a new Java class. Though our program is simple and doesn’t require importing external packages, it’s good practice to know how to include them for more complex projects.
Here is the basic Java program:
import java.util.Scanner;
public class CheckEvenOdd {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = reader.nextInt();
if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}
The if statement checks the condition number % 2 == 0
. The modulo operator % returns the remainder of dividing number by 2. If the remainder is 0, the number is even; if not, it’s odd. This simple check forms the basis of our program.
Running the Program
With the code in place, compile and run your Java program. Enter a number when prompted, and the program will tell you whether it’s odd or even. This interaction provides immediate feedback and reinforces your understanding of both Java syntax and basic programming logic.
Save the above content in a file CheckEvenOdd.java. Then execute the following commands to compile the java program.
javac CheckEvenOdd.java
Once the file compiled successfully, it will generate a class file named CheckEvenOdd.class. Now run this application with command:
java CheckEvenOdd
This will prompt you to enter a number. Then program will check if given number is even or odd and show result accordingly.
Conclusion
Building a Java program to check if a number is odd or even might seem elementary, but it encapsulates essential programming principles such as input handling, conditional statements, and arithmetic operations. For beginners, mastering such simple programs lays the groundwork for tackling more challenging projects. As you progress, consider enhancing the program—perhaps by incorporating a loop to allow continuous number checking or by designing a graphical user interface (GUI) for a more user-friendly experience. In programming, every small step is a leap towards mastery. Happy coding!