Find the Age Group in C / C++ / Java / Python / C#

Age groups

Programs to find the age group of a given age are shown here. The age group is calculated according to the age provided by the user. The following table depicts the age groups and the ranges of the ages.


Age Group Age Ranges
Baby More than 0 to below 2
Child 2 to below 13
Adolescent 13 to below 18
Adult 18 to below 60
Old 60 or more

For example, if the age of person A and person B are 45 and 10 respectively, then person A is an adult and person B is a child.






1. Algorithm to find the age group


1. Take the age as input.

2. If the age is less than or equal to 0, display the age as invalid.

3. If the age is more than 0 and below 2, display the age group "Baby" as the output.

4. If the age is between 2 and below 13, display the age group "Child" as the output.

5. If the age is between 13 and below 18, display the age group "Adolescent" as the output.

6. If the age is between 18 and below 60, display the age group "Adult" as the output.

7. If the age is 60 or more, display the age group "Old" as the output.




2. Pseudocode to find the age group


Input : An age value $s$

Output : The age group of $s$

1. Procedure ageGroup($s$):

2. If $s \leq 0$:

3. Return "Invalid age!"

4. Else If $s < 2$:

5. Return "Baby"

6. Else If $s < 13$:

7. Return "Child"

8. Else If $s < 18$:

9. Return "Adolescent"

10. Else If $s < 60$:

11. Return "Adult"

12. Else:

13. Return "Old"

14. End Procedure





3. Time complexity to find the age group


Time Complexity: O(1)





4. Program to find the age group

In this section, the C, C++, Java, Python and C# programs to find the age group w.r.t the input age are given. A valid age should be greater than 0.




4.1. C Program to find the age group

Code has been copied
/********************************
        alphabetacoder.com
C program to find the age group
*********************************/
#include <stdio.h>

int main() {
    // declare variables
    double age;

    // take input
    printf("Enter the age: ");
    scanf("%lf", & age);

    // find the grade
    if (age <= 0)
        printf("Invalid age!");
    else if (age < 2)
        printf("Age group: Baby");
    else if (age < 13)
        printf("Age group: Child");
    else if (age < 18)
        printf("Age group: Adolescent");
    else if (age < 60)
        printf("Age group: Adult");
    else
        printf("Age group: Old");

    return 0;
}

Output


Case 1:

Enter the age: 95

Age group: Old


Case 2:

Enter the age: 1.5

Age group: Baby


Case 3:

Enter the age: 11

Age group: Child




4.2. C++ Program to find the age group

Code has been copied
/********************************
        alphabetacoder.com
C++ program to find the age group
*********************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    double age;

    // take input
    cout << "Enter the age: ";
    cin >> age;

    // find the grade
    if (age <= 0)
        cout << "Invalid age!";
    else if (age < 2)
        cout << "Age group: Baby";
    else if (age < 13)
        cout << "Age group: Child";
    else if (age < 18)
        cout << "Age group: Adolescent";
    else if (age < 60)
        cout << "Age group: Adult";
    else
        cout << "Age group: Old";

    return 0;
}

Output


Case 1:

Enter the age: 45

Age group: Adult


Case 2:

Enter the age: 22.4

Age group: Adult


Case 3:

Enter the age: 15

Age group: Adolescent




4.3. Java Program to find the age group

Code has been copied
/**********************************
        alphabetacoder.com
Java program to find the age group
***********************************/

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        // declare variables
        double age;

        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // take input
        System.out.print("Enter the age: ");
        age = sc.nextDouble();

        // find the grade
        if (age <= 0)
            System.out.print("Invalid age!");
        else if (age < 2)
            System.out.print("Age group: Baby");
        else if (age < 13)
            System.out.print("Age group: Child");
        else if (age < 18)
            System.out.print("Age group: Adolescent");
        else if (age < 60)
            System.out.print("Age group: Adult");
        else
            System.out.print("Age group: Old");
    }
}

Output


Case 1:

Enter the age: 77

Age group: Old


Case 2:

Enter the age: -2

Invalid age!


Case 3:

Enter the age: 18

Age group: Adult




4.4. Python Program to find the age group

Code has been copied
# ************************************
#        alphabetacoder.com
# Python program to find the age group
# ************************************

# take input
age = float(input("Enter the age: "))

# find the grade
if age <= 0:
    print("Invalid age!")
elif age < 2:
    print("Age group: Baby")
elif age < 13:
    print("Age group: Child")
elif age < 18:
    print("Age group: Adolescent")
elif age < 60:
    print("Age group: Adult")
else:
    print("Age group: Old")

Output


Case 1:

Enter the age: 32

Age group: Adult


Case 2:

Enter the age: 14

Age group: Adolescent


Case 3:

Enter the age: 13

Age group: Adolescent




4.5. C# Program to find the age group

Code has been copied
/********************************
        alphabetacoder.com
C# program to find the age group
*********************************/

using System;

namespace AgeGroup {
    class Program {
        static void Main(string[] args) {
            // declare variables
            double age;

            // take input
            Console.Write("Enter the age: ");
            age = Convert.ToDouble(Console.ReadLine());

            // find the grade
            if (age <= 0)
                Console.WriteLine("Invalid age!");
            else if (age < 2)
                Console.WriteLine("Age group: Baby");
            else if (age < 13)
                Console.WriteLine("Age group: Child");
            else if (age < 18)
                Console.WriteLine("Age group: Adolescent");
            else if (age < 60)
                Console.WriteLine("Age group: Adult");
            else
                Console.WriteLine("Age group: Old");

            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Case 1:

Enter the age: 40

Age group: Adult


Case 2:

Enter the age: 16.5

Age group: Adolescent


Case 3:

Enter the age: 1

Age group: Baby