Java program to check eligibility for voting
In this post, you will learn how to check if a user is eligible for vote or not. The given Java program will accept the user's age as input, check his eligibility, and then print out the result.
In India, a person's age should be 18 or greater than 18 to become eligible for voting. We can simply implement this logic in an if else conditional statement to check whether a person is eligible to vote or not.
Java program to check vote eligibility
In this program, we check the voting age using an if-else statement. First, we declare and initialise the required variables. Next, we prompt the user to input the age value. Later, we calculate whether the age is valid for voting or not.
// Java program to check vote eligibility
import java.util.Scanner;
public class Voting {
public static void main(String[] args)
{
// Declaring variables
int age, diff;
// taking user input
Scanner scan = new Scanner(System.in);
System.out.println("Please enter your age: ");
age = scan.nextInt();
// Checking condition for voting..
if(age>=18)
{
System.out.println("You are eligible for voting.");
}
else
{
diff = (18 - age);
System.out.println("Sorry,You can vote after: "+ diff + " years");
}
}
}
Output1:
Please enter your age:
20
You are eligible for voting.
Output2:
Please enter your age:
15
Sorry,You can vote after 3 years.
Output3:
Please enter your age:
18
You are eligible for voting.
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java