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?

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.

Consider the program:

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





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.