Write a program to check whether a person is eligible to vote or Not using switch statement


This Java program checks whether a person is eligible to vote based on their age. The program prompts the user to enter their age and uses a ternary operator to check whether the age is greater than or equal to 18. If the age is greater than or equal to 18, the program prints "You are Eligible for Voting". Otherwise, the program prints "You are Not Eligible for Voting". Here's how the program works:

  • The program prompts the user to enter their age using System.out.printf() and Scanner.nextInt() method, which is stored in the variable age.
  • The program uses a ternary operator to check whether the age is greater than or equal to 18. If it is, the value of the variable res is set to 1. Otherwise, it is set to 0.
  • The program uses a switch statement to print the corresponding message based on the value of res.

Overall, this program is a simple example of using ternary operator and switch statement in Java to check eligibility for voting based on age.

Source Code

import java.util.Scanner;
public class Vote_Eligible
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		int age = 0;
		System.out.printf("Enter the Age : ");
		age = input.nextInt();
		int res = age >= 18?1:0;
		switch (res)
		{
			case 0:
				System.out.printf("You are Not Eligible for Voting...");
				break;
 
			case 1:
				System.out.printf("You are Eligible for Voting...");
				break;
		}
	}
}

Output

Enter the Age : 23
You are Eligible for Voting...