Age Categorizer

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 if statements 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 if age < 0 is false. We know that age is between 0 and 2 if age < 2 evaluates as true.

The same thing goes for the following statements.

Questions