Exercise
Greatest of three numbers
Objetive
Write a java program that prompts the user to enter three numbers and displays the greatest one.
Example Code
import java.util.Scanner; // Import Scanner class for input
public class Program {
// Main entry point of the program
public static void main(String[] args) {
// Create a scanner object to read user input
Scanner scanner = new Scanner(System.in);
// Ask the user to input the first number
System.out.print("Enter the first number: ");
// Read the first number as a double
double firstNumber = scanner.nextDouble();
// Ask the user to input the second number
System.out.print("Enter the second number: ");
// Read the second number as a double
double secondNumber = scanner.nextDouble();
// Ask the user to input the third number
System.out.print("Enter the third number: ");
// Read the third number as a double
double thirdNumber = scanner.nextDouble();
// Determine the greatest number using conditional statements
double greatest = firstNumber;
if (secondNumber > greatest) {
greatest = secondNumber;
}
if (thirdNumber > greatest) {
greatest = thirdNumber;
}
// Display the greatest number
System.out.println("The greatest number is: " + greatest);
// Close the scanner to free up resources
scanner.close();
}
}