Home »
C++ programming language
Example of if-else statement in C++ (Input age and check voting eligibility and teenage)
Example of if-else statement in C++: In this program, we will take age of a persona and checking person is teenager or not and he is eligible for voting or not?
Problem statement
Given age of a person and we have to check voting edibility and check person is teenager or not.
Teenage validation
If person’s age is greater than or equal to 13 and less than or equal to 19, than person is teenager otherwise person is not a teenager.
Voting edibility
If person’s age is greater than or equal to 18, person is eligible for voting.
C++ - Input age and check voting eligibility and teenage
Here, we are using two conditions one (age>=13 && age<=19) for teenage validation and second (age>=18) for voting eligibility.
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
// person is teenager or not
//>=13 and <=19
if (age >= 13 && age <= 19) {
cout << "Person is Teenager" << endl;
} else {
cout << "Person is not a Teenager" << endl;
}
// condition to check voting eligility
if (age >= 18) {
cout << "Personl is eligible for voting" << endl;
} else {
cout << "Person is not eligible for voating" << endl;
}
return 0;
}
Output
First run:
Enter your age: 19
Person is Teenager
Personl is eligible for voting
Second run:
Enter your age: 14
Person is Teenager
Person is not eligible for voating
Third run:
Enter your age: 21
Person is not a Teenager
Personl is eligible for voting