Java program to take age and tell whether its infant or child or adult
Following program shows you how to take age and tell whether its infant or child or adult.
In this program we get age from user, and if the age is less than or equal to 0 it prints invalid input, and if the age is between 1 to 5 it shows infant and if the age is between 6 to 10 it prints child otherwise it prints adult.
import java.util.Scanner;
public class Basic4 {
public static void main(String[] args) {
System.out.println("Please enter your age:");
Scanner in = new Scanner(System.in);
int age = in.nextInt();
if (age <= 0) {
System.out.println("Invalid input");
} else if (age >= 1 && age <= 5) {
System.out.println("Infant");
} else if (age >= 6 && age <= 10) {
System.out.println("Child");
} else {
System.out.println("Adult");
}
}
}
Output:
Example1:
Please enter your age:
2
Infant
Example2:
Please enter your age:
8
Child
Example3:
Please enter your age:
50
Adult
Example4:
Please enter your age:
-1
Invalid input