Java Online Compiler
Example: Greatest of Two Numbers using if-else in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Greatest of Two Numbers using if-else 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 to read user input Scanner scanner = new Scanner(System.in); // Step 2: Prompt the user to enter the first number System.out.print("Enter the first number: "); int num1 = scanner.nextInt(); // Step 3: Prompt the user to enter the second number System.out.print("Enter the second number: "); int num2 = scanner.nextInt(); // Step 4: Compare the two numbers using an if-else statement if (num1 > num2) { System.out.println(num1 + " is the greatest number."); } else { System.out.println(num2 + " is the greatest number."); } // Step 5: Close the scanner to prevent resource leaks scanner.close(); } }
Output
Clear
ADVERTISEMENTS