In this program, we’ll take the user’s age as input and check whether the person is eligible to vote or not.
In India, the minimum age required to vote is 18 years.
๐ป Program Code
// Java Program to Check Voting Eligibility
import java.util.Scanner;
public class VotingEligibility {
public static void main(String[] args) {
// Create Scanner object to take input from user
Scanner sc = new Scanner(System.in);
// Ask user to enter age
System.out.print("Enter your age: ");
int age = sc.nextInt();
// Check eligibility
if (age >= 18) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote yet.");
System.out.println("You can vote after " + (18 - age) + " year(s).");
}
sc.close(); // Close the scanner
}
}
๐ง Explanation
-
import java.util.Scanner;
→ Imports the Scanner class to take input from the user. -
int age = sc.nextInt();
→ Reads an integer value (age) from the user. -
if (age >= 18)
→ Checks if the person is 18 or older. -
elseblock
→ Runs when the user’s age is below 18 and tells how many years remain to vote.
๐งฉ Sample Output
Example 1:
Enter your age: 20
You are eligible to vote.
Example 2:
Enter your age: 16
You are not eligible to vote yet.
You can vote after 2 year(s).
๐ Conclusion
This simple Java program helps determine whether a person can vote based on their age.
You can enhance it by checking additional conditions like citizenship, ID verification, etc.
Comments
Post a Comment