Page 52 - CTS - CSA TP - Volume 2
P. 52
COMPUTER SOFTWARE APPLICATION - CITS
• It reads the integer input provided by the user using the nextInt() method of the Scanner class and stores it in
the variable number.
4 The program then checks whether the entered number is even or odd using the modulo operator %.
• If the remainder of number divided by 2 is equal to 0, then the number is even.
• If the remainder is not equal to 0, then the number is odd.
5 Depending on the result of the check, the program prints out a message indicating whether the number is even
or odd.
6 Finally, the scanner object is closed to release system resources after it’s no longer needed.
Output:
TASK 2: Checking if a person is eligible to vote
import java.util.Scanner;
public class VotingEligibilityCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter your age: “);
int age = scanner.nextInt();
if (age >= 18) {
System.out.println(“You are eligible to vote.”);
} else {
System.out.println(“You are not eligible to vote yet.”);
}
scanner.close}}
Explanation:
This Java program, named VotingEligibilityCheck, determines whether a person is eligible to vote based on their
age. Here’s a brief explanation of how it works:
1 The program starts by importing the Scanner class from the java.util package, which allows user input from the
console.
2 The VotingEligibilityCheck class contains the main method, which serves as the entry point of the program.
37
CITS : IT & ITES - Computer Software Application - Exercise 85