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
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$:

10. Else If $s < 60$:

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);

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)
else if (age < 60)
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;

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)
else if (age < 60)
else
cout << "Age group: Old";

return 0;
}


Output

#### Case 1:

Enter the age: 45

#### Case 2:

Enter the age: 22.4

#### Case 3:

Enter the age: 15

## 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();

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)
else if (age < 60)
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

## 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: "))

if age <= 0:
print("Invalid age!")
elif age < 2:
print("Age group: Baby")
elif age < 13:
print("Age group: Child")
elif age < 18:
elif age < 60:
else:
print("Age group: Old")


Output

#### Case 1:

Enter the age: 32

#### Case 2:

Enter the age: 14

#### Case 3:

Enter the age: 13

## 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: ");

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)
else if (age < 60)
else
Console.WriteLine("Age group: Old");

// wait for user to press any key
}
}
}


Output

#### Case 1:

Enter the age: 40

#### Case 2:

Enter the age: 16.5