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