Introduction
In this post, I am going to write a java program to find whether a person is eligible to vote or not. In India, you must be 18 years old or more to vote in the elections. I am not aware of the other countries.
Also Read: Sum of Odd and Even Digits in a Number in Java
So, in this program, I am considering the minimum age for voting is 18 years old. Here, I have also considered a maximum age also i.e. 100 years old. I am not an astrologer who predicts the maximum age of the person. But, it is just a maximum value. You can take anything as per your wish.
Before writing the program, let us see the expected output for this program.
Output – Eligible to Vote

Output – Not Eligible to Vote

In both the outputs, I have taken two different persons of different ages. Our main criteria are used to compare the age. If the age is less than 18, then that boy or girl or person is not eligible for voting.
Let us see the actual java program.
Java Program to Find Whether a Person is Eligible to Vote or Not
I have written this program using a notepad editor. You can use any editor.
import java.util.*; class Voting { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter your Name: "); String name=sc.nextLine(); System.out.println("Enter your age: "); int age=sc.nextInt(); if((age>=18)&&(age<=100)) { System.out.println("Congratulation "+name+", You are eligible for Voting"); } else { System.out.println("Sorry "+name+", You are not eligible for voting"); } } }
In this program, we are reading the values from the console using the Scanner class. For using this class, you must import the util package as shown in the above program. These nextLine() and nextInt() are two important methods of this Scanner class. The nextLine() reads the entire line of text or string and nextInt() reads the integer value from the console.
- First we are asking the user to enter his name. This is not compulsory to ask the name.
- After reading the name, we are asking the user to enter his name so that this program can compare this value with the value 18 i.e. minimum age for voting.
I hope you have understood this program. If you have any difficulty then please feel free to contact me.
Thank you.