Program to check if a person can vote using his age | Menu-Driven
Last Updated :
14 Feb, 2024
Write a Menu-driven program to check the eligibility of a person to vote or not.
Eligibility to vote: The age of the person should be greater than or equal to 18.
A menu-driven program is a type of computer program that allows users to interact with it by selecting options from a menu. Instead of typing commands or code, users navigate through a series of menus that present a list of actions or functions they can perform.
Examples:
Enter your age: 12
Person is not allowed to vote
To continue press Y
To exit press N
Y
Enter your age: 19
Person is allowed to vote
To continue press Y
To exit press N
N
Program Terminated
Approach: Using While looping and If-Else conditioning
The problem can be solved using a while loop which keeps on asking the user to input the age, till the user does not press 'N'. Inside the while loop, the user enters the age and if the age < 18, the user is not eligible to vote else the user is eligible. As soon as the user presses 'N', the control comes out of the while loop and the program gets terminated.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
char input = 'Y';
int age;
while (input == 'Y') {
cout << "Enter your age: " << endl;
cin >> age;
if (age >= 18) {
cout << "Person is allowed to vote" << endl;
}
else {
cout << "Person is not allowed to vote" << endl;
}
cout << "To continue press Y" << endl;
cout << "To exit press another key" << endl;
cin >> input;
}
cout << "Program Terminated" << endl;
}
Java
import java.util.Scanner;
public class VotingProgram {
public static void main(String[] args) {
// Create a Scanner object for taking input from the user
Scanner scanner = new Scanner(System.in);
// Initialize input with 'Y' to start the loop
char input = 'Y';
// Loop until the user enters a key other than 'Y'
while (input == 'Y') {
// Prompt the user to enter their age
System.out.println("Enter your age:");
// Read the age input from the user
int age = scanner.nextInt();
// Check if the person is allowed to vote based on age
if (age >= 18) {
System.out.println("Person is allowed to vote");
} else {
System.out.println("Person is not allowed to vote");
}
// Prompt the user to continue or exit
System.out.println("To continue press Y");
System.out.println("To exit press another key");
// Read the next character input from the user
input = scanner.next().charAt(0);
}
// Display a termination message when the loop exits
System.out.println("Program Terminated");
// Close the Scanner to prevent resource leaks
scanner.close();
}
}
Python3
while True:
# Get user input for age
age = int(input("Enter your age: "))
# Check if the person is allowed to vote based on age
if age >= 18:
print("Person is allowed to vote")
else:
print("Person is not allowed to vote")
# Ask the user if they want to continue
input_str = input("To continue, press 'Y'. To exit, press another key: ")
# Convert the input to uppercase for case-insensitivity
input_char = input_str.upper()
# Check if the user wants to continue
if input_char != 'Y':
break # Exit the loop if the input is not 'Y'
# Print a message when the program is terminated
print("Program Terminated")
C#
using System;
class Program
{
static void Main(string[] args)
{
char input = 'Y'; // Initialize input variable with 'Y'
int age; // Declare age variable
while (input == 'Y') // Loop while input is 'Y'
{
Console.WriteLine("Enter your age: "); // Prompt for age input
age = Convert.ToInt32(Console.ReadLine()); // Read age input from the console
if (age >= 18) // Check if age is greater than or equal to 18
{
Console.WriteLine("Person is allowed to vote"); // Display message if allowed to vote
}
else
{
Console.WriteLine("Person is not allowed to vote"); // Display message if not allowed to vote
}
Console.WriteLine("To continue press Y"); // Prompt for continuation
Console.WriteLine("To exit press another key"); // Prompt for exit
input = Convert.ToChar(Console.ReadLine()); // Read input for continuation or exit
}
Console.WriteLine("Program Terminated"); // Display termination message
}
}
JavaScript
const readline = require('readline'); // Importing the readline module to read user input
const rl = readline.createInterface({ // Creating an interface to read input
input: process.stdin,
output: process.stdout
});
function checkVotingEligibility() {
rl.question('Enter your age: ', (age) => { // Asking user to enter their age
if (age >= 18) {
console.log('Person is allowed to vote'); // If age is 18 or above, person is allowed to vote
} else {
console.log('Person is not allowed to vote'); // If age is below 18, person is not
// allowed to vote
}
rl.question('To continue press Y\nTo exit press another key\n', (input) => {
if (input.toUpperCase() === 'Y') { // Checking if input is 'Y' (case-insensitive)
checkVotingEligibility(); // If 'Y' is entered, continue to prompt for age
} else {
console.log('Program Terminated'); // If any other key is entered, terminate the program
rl.close(); // Close the readline interface
}
});
});
}
checkVotingEligibility(); // Calling the function to start the process
Output:
Enter your age: 12
Person is not allowed to vote
To continue press Y
To exit press N
Y
Enter your age: 19
Person is allowed to vote
To continue press Y
To exit press N
N
Program Terminated
Similar Reads
Check if all people can vote on two machines There are n people and two identical voting machines. We are also given an array a[] of size n such that a[i] stores time required by i-th person to go to any machine, mark his vote and come back. At one time instant, only one person can be there on each of the machines. Given a value x, defining th
14 min read
Program to check if a date is valid or not Given a date, check if it is valid or not. It may be assumed that the given date is in range from 01/01/1800 to 31/12/9999. Examples : Input : d = 10, m = 12, y = 2000 Output : Yes The given date 10/12/2000 is valid Input : d = 30, m = 2, y = 2000 Output : No The given date 30/2/2000 is invalid. The
8 min read
Distribution of candies according to ages of students Given two integer arrays ages and packs where ages store the ages of different students and an element of pack stores the number of candies that packet has (complete array represent the number of packets). The candies can be distributed among students such that: Every student must get only one pack
11 min read
Check if a given Year is Leap Year You are given an Integer n. Return true if It is a Leap Year otherwise return false. A leap year is a year that contains an additional day, February 29th, making it 366 days long instead of the usual 365 days. Leap years are necessary to keep our calendar in alignment with the Earth's revolutions ar
4 min read
Program to calculate age Given the current date and birth date, find the present age. Examples: Input : Birth date = 07/09/1996 Present date = 07/12/2017 Output : Present Age = Years: 21 Months: 3 Days: 0 t Age = Years: 7 Months: 11 Days: 21 While calculating the difference in two dates we need to just keep track of two con
9 min read