Find Youngest Age in Three in Java


If the ages of Ram, Shyam and Ajay are input through the keyboard, write a program to determine the youngest of the three


The program starts by creating a Scanner object to read input from the user. It then prompts the user to enter the age of Ram, Shyam, and Ajay one by one using the nextInt() method of the Scanner class.

Next, the program compares the ages of Ram, Shyam, and Ajay using conditional statements. It uses the logical AND (&&) operator to check whether Ram's age is less than both Shyam's and Ajay's ages. If so, it prints "The Youngest Age is Ram". Otherwise, it checks whether Shyam's age is less than both Ram's and Ajay's ages. If so, it prints "The Youngest Age is Shyam". If neither of these conditions is true, it concludes that Ajay is the youngest and prints "The Youngest Age is Ajay".

Note that the program assumes that the ages entered by the user are positive integers. If the user enters negative values or non-integer values, the program may produce unexpected results or throw an exception.

Source Code

import java.util.Scanner;
class Youngest
{
	public static void main(String args[])
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the Age of Ram :");
		int age1  = input.nextInt();
		System.out.print("Enter the Age of Shyam  :");
		int age2  = input.nextInt();
		System.out.print("Enter the Age of Ajay  :");
		int age3  = input.nextInt();
		if(age1<age2 && age1<age3)
			System.out.print("The Youngest Age is Ram");
		else if(age2<age1 && age2<age3)			
			System.out.print("The Youngest Age is Shyam");
		else			
			System.out.print("The Youngest Age is Ajay");
	}
}

Output

Enter the Age of Ram :24
Enter the Age of Shyam  :26
Enter the Age of Ajay  :21
The Youngest Age is Ajay

Example Programs