Programming Examples

Java program to find category based on age using ternary operator


Write a Java program that takes a person's age as input and determines their age category. The categories are as follows:

"Child" for ages 0-12

"Teen" for ages 13-19

"Adult" for ages 20-59

"Senior" for ages 60 and above

Solution

import java.util.Scanner;
public class AgeCategory 
{
    public static void main(String args[]) 
	{
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your age: ");
        int age = sc.nextInt();
        String category = (age >= 0 && age <= 12) ? "Child" :
                          (age >= 13 && age <= 19) ? "Teen" :
                          (age >= 20 && age <= 59) ? "Adult" :
                          (age >= 60) ? "Senior" : "Invalid age";
        System.out.println("The age category is: " + category);
    }
}
Output

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad