In this tutorial we are writing a C++ Program to Check Eligibility for voting using function.
To check that a person is eligible for voting or not, we need to check whether person’s age is greater than or equal to 18. For this we are reading age in a variable a and checking the condition a>=18, if the condition is true, “person will be eligible for voting” else not.
Program Source Code : C++ program to check eligibility for voting using function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> using namespace std; string voteAge(int age) { string str = ""; if(age >= 18) str = "Eligible for Voting! "; else str = "Not Eligible for Voting! "; return str; } int main() { int age; cout<<"\nEnter age"; cin>>age; cout << voteAge(age) << endl; return 0; } |
Output:
1 2 3 4 |
Enter age25 Eligible for Voting! |
This program is checking whether a person is eligible for voting or not based on their age.
It has a function called “voteAge” which takes an integer argument “age”.
Inside the function, it checks if the age is greater than or equal to 18, then sets the string “str” to “Eligible for Voting!”.
If the age is less than 18, the string “str” is set to “Not Eligible for Voting!”.
The function then returns the value of the “str” string.
In the main function, it asks the user to input their age, then prints the result returned from the voteAge function.