Java Online Compiler
Example: Greatest of Three Numbers using Nested If in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Greatest of Three Numbers using Nested If import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Initialize Scanner for input Scanner input = new Scanner(System.in); // Step 2: Prompt user for three numbers System.out.print("Enter first number: "); int num1 = input.nextInt(); System.out.print("Enter second number: "); int num2 = input.nextInt(); System.out.print("Enter third number: "); int num3 = input.nextInt(); // Step 3: Implement nested if-else logic to find the greatest int greatest; if (num1 >= num2) { // If num1 is greater than or equal to num2, compare num1 with num3 if (num1 >= num3) { greatest = num1; // num1 is the greatest } else { greatest = num3; // num3 is the greatest } } else { // If num2 is greater than num1, compare num2 with num3 if (num2 >= num3) { greatest = num2; // num2 is the greatest } else { greatest = num3; // num3 is the greatest } } // Step 4: Display the result System.out.println("The greatest number is: " + greatest); // Step 5: Close the scanner input.close(); } }
Output
Clear
ADVERTISEMENTS