Introduction :
This Java program is designed to determine whether a given integer is even or odd. It interacts with the user by prompting them to enter a number, then performs a simple calculation to check if the number is divisible by 2. Depending on the result, it outputs whether the number is even or odd.
Algorithm:
- Import the Scanner class to facilitate user input.
- Create a class named EvenOrOdd.
- In the main method: a. Initialize a Scanner object for user input. b. Display a prompt asking the user to enter a number. c. Read the entered number using nextInt() method of Scanner. d. Check if the entered number is divisible by 2 (even) using the modulo operator (%). e. If the remainder is 0, print that the number is even; otherwise, print that it’s odd. f. Close the Scanner to free up resources.
Explanation:
- The program begins by importing the Scanner class, which allows the user to input values during runtime.
- The EvenOrOdd class is created to encapsulate the program logic.
- Inside the main method, a Scanner object named scanner is instantiated.
- The user is prompted to input a number using System.out.print(“Enter a number: “).
- The entered number is then stored in the variable number.
- The program checks if number % 2 == 0, where % is the modulo operator. If the result is 0, the number is even; otherwise, it’s odd.
- The program prints the appropriate message based on the result.
- Finally, the Scanner is closed to prevent resource leaks.
Code:
import java.util.Scanner;
public class EvenOrOdd {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
scanner.close();
}
}
Input :
Enter a number: 65
Output :
65 is odd.
This program takes input from the user, checks whether the entered number is even or odd, and then prints the result. In summary, this program is a basic example of user interaction, conditional statements, and arithmetic operations in Java, showcasing how to determine if a given number is even or odd.
FAQs :
Q: How does the program determine if a number is even or odd in Java?
A: The program uses the modulo operator (%), which calculates the remainder when dividing the entered number by 2. If the remainder is 0, the number is even; otherwise, it’s odd.
Q: Can I enter any type of number, including decimals?
A: No, the program specifically expects integer input. If you enter a decimal, the program may not behave as expected, as it uses the nextInt() method of the Scanner class.
Q: What happens if I input a non-numeric value?
A: The program will throw an InputMismatchException if a non-integer value is entered. To handle this, you may need to implement additional input validation.
Q: Is there a limit to the size of the number I can input?
A: The program works with integers within the range of Java’s int data type. If the entered number exceeds the maximum or minimum value for an int, the program may not provide accurate results.
Q: Can I modify the program to check for even or odd for multiple numbers in a single run?
A: Yes, you can modify the program by using loops to repeatedly ask the user for input and check whether each entered number is even or odd. This way, you can analyze multiple numbers in a single program execution.