Open In App

Program to check if a person can vote using his age | Menu-Driven

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads