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) {
Scanner scanner = new Scanner(System.in);
char input = 'Y' ;
while (input == 'Y' ) {
System.out.println( "Enter your age:" );
int age = scanner.nextInt();
if (age >= 18 ) {
System.out.println( "Person is allowed to vote" );
} else {
System.out.println( "Person is not allowed to vote" );
}
System.out.println( "To continue press Y" );
System.out.println( "To exit press another key" );
input = scanner.next().charAt( 0 );
}
System.out.println( "Program Terminated" );
scanner.close();
}
}
|
Python3
while True :
age = int ( input ( "Enter your age: " ))
if age > = 18 :
print ( "Person is allowed to vote" )
else :
print ( "Person is not allowed to vote" )
input_str = input ( "To continue, press 'Y'. To exit, press another key: " )
input_char = input_str.upper()
if input_char ! = 'Y' :
break
print ( "Program Terminated" )
|
C#
using System;
class Program
{
static void Main( string [] args)
{
char input = 'Y' ;
int age;
while (input == 'Y' )
{
Console.WriteLine( "Enter your age: " );
age = Convert.ToInt32(Console.ReadLine());
if (age >= 18)
{
Console.WriteLine( "Person is allowed to vote" );
}
else
{
Console.WriteLine( "Person is not allowed to vote" );
}
Console.WriteLine( "To continue press Y" );
Console.WriteLine( "To exit press another key" );
input = Convert.ToChar(Console.ReadLine());
}
Console.WriteLine( "Program Terminated" );
}
}
|
Javascript
const readline = require( 'readline' );
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function checkVotingEligibility() {
rl.question( 'Enter your age: ' , (age) => {
if (age >= 18) {
console.log( 'Person is allowed to vote' );
} else {
console.log( 'Person is not allowed to vote' );
}
rl.question( 'To continue press Y\nTo exit press another key\n' , (input) => {
if (input.toUpperCase() === 'Y' ) {
checkVotingEligibility();
} else {
console.log( 'Program Terminated' );
rl.close();
}
});
});
}
checkVotingEligibility();
|
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
Share your thoughts in the comments
Please Login to comment...