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.

using namespace std;

int main()
	int age;

	cout<<"Enter your age: ";

	//person is teenager or not
	//>=13 and <=19
	if(age>=13 && age<=19)
		cout<<"Person is Teenager"<<endl;
		cout<<"Person is not a Teenager"<<endl;

	//condition to check voting eligility
		cout<<"Personl is eligible for voting"<<endl;
		cout<<"Person is not eligible for voating"<<endl;

	return 0;


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

Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.