Age Category
Using else if statements, make a program that prompts the user for someones age and determines whether the user is a baby(0-2), a child(3-12), a teenage (13-19), an adult (20-65), or a serior (65+).
If the number is a negative number output "unborn".
Answer
#include <iostream>
int main()
{
std::cout << "Give me the age of a person:\n";
int age;
std::cin >> age;
if (age < 0)
std::cout << "This person is unborn.";
else if (age < 3)
std::cout << "This person is a baby.";
else if (age < 13)
std::cout << "This person is a child.";
else if (age < 20)
std::cout << "This person is a teenager.";
else if (age < 65)
std::cout << "This person is an adult.";
else
std::cout << "This person is a senior.";
}
Since we list the
else ifstatements in order of accending age we don't have to check if the age is between 2 numbers.else if (age < 2)is only reached ifage < 0isfalse. We know thatageis between0and2ifage < 2evaluates as true.The same thing goes for the following statements.