Java Online Compiler
Example: GreatestOfThreeUsingMathMax in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// GreatestOfThreeUsingMathMax import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Create a Scanner object Scanner scanner = new Scanner(System.in); // Step 2: Get three numbers from the user System.out.print("Enter the first number: "); int num1 = scanner.nextInt(); System.out.print("Enter the second number: "); int num2 = scanner.nextInt(); System.out.print("Enter the third number: "); int num3 = scanner.nextInt(); // Step 3: Use Math.max() to find the greatest number int greatest = Math.max(num1, Math.max(num2, num3)); // Step 4: Print the result System.out.println(greatest + " is the greatest number."); // Step 5: Close the scanner scanner.close(); } }
Output
Clear
ADVERTISEMENTS