Java Online Compiler
Example: Largest of Three Numbers using Logical AND in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Largest of Three Numbers using Logical AND import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Declare variables to store the three numbers int num1, num2, num3; // Step 2: Create a Scanner object to get user input Scanner scanner = new Scanner(System.in); // Step 3: Prompt the user to enter the three numbers System.out.print("Enter the first number: "); num1 = scanner.nextInt(); System.out.print("Enter the second number: "); num2 = scanner.nextInt(); System.out.print("Enter the third number: "); num3 = scanner.nextInt(); // Step 4: Determine the largest number using if-else if-else with logical AND if (num1 >= num2 && num1 >= num3) { System.out.println("The largest number is: " + num1); } else if (num2 >= num1 && num2 >= num3) { System.out.println("The largest number is: " + num2); } else { // If neither num1 nor num2 is the largest, num3 must be System.out.println("The largest number is: " + num3); } // Step 5: Close the scanner to prevent resource leak scanner.close(); } }
Output
Clear
ADVERTISEMENTS