149k views
1 vote
Write a program that asks the user to enter a person’s age. The program should display a message indicating whether the person is an infant, a child, a teenager, or an adult. Following are the guidelines: If the person is 1 year old or less, he or she is an infant. If the person is older than 1 year, but younger than 13 years, he or she is a child. If the person is at least 13 years old, but less than 20 years old, he or she is a teenager. If the person is at least 20 years old, he or she is an adult.

1 Answer

4 votes

Answer:

The c++ program for the given scenario is given below.

#include <iostream>

using namespace std;

int main() {

int age;

string person;

cout << "This program prints whether the person is an infant, a child, a teenager or an adult based on the age entered by the user." << endl;

cout << "Enter the age in completed years" << endl;

cin >> age;

if( age>=0 && age<=1 )

person = "infant";

else if( age>1 && age<13 )

person = "child";

else if( age>=13 && age<20 )

person = "teenager";

else

person = "adult";

cout << "The person is " << person << " at the age of " << age << endl;

return 0;

}

OUTPUT

This program prints whether the person is an infant, a child, a teenager or an adult based on the age entered by the user.

Enter the age in completed years

1

The person is infant at the age of 1

Step-by-step explanation:

This program prints the phase of the human life based on the age entered by the user. The program executes as follows.

1. User is prompted to enter the age in completed years. The program does not validates the user inputted age since it is not specifically mentioned in the question.

2. An integer variable, age, is declared which is initialized to the age entered by the user.

3. A string variable, person, is declared which is initialized to the phase of the person based on the entered age. The variable person is not initialized.

4. The age of the user is tested using multiple if else statements. Based on the age criteria given in the question, the variable person is assigned a value. This value represents the phase of the person which is equivalent to the age.

5. The program terminates with a return statement since the main method has the return type of integer.

User Seaneshbaugh
by
5.6k points