Largest number in Java


Q. Write a Java program to find the largest number among three numbers using simple if statement

import java.util.Scanner;

public class LargestOfThree {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Read three numbers from the user
        System.out.print("Enter first number: ");
        int num1 = scanner.nextInt();

        System.out.print("Enter second number: ");
        int num2 = scanner.nextInt();

        System.out.print("Enter third number: ");
        int num3 = scanner.nextInt();

        // Use simple if statements to find the largest
        if (num1 >= num2 && num1 >= num3) {
            System.out.println(num1 + " is the largest number.");
        }

        if (num2 >= num1 && num2 >= num3) {
            System.out.println(num2 + " is the largest number.");
        }

        if (num3 >= num1 && num3 >= num2) {
            System.out.println(num3 + " is the largest number.");
        }

        scanner.close();
    }
}
 

Q. Write an algorithm to find the largest number among three numbers using simple if statement

1. Start
2. Read three numbers: num1, num2, num3
3. If num1 >= num2 and num1 >= num3, then
    → Print num1 is the largest
4. If num2 >= num1 and num2 >= num3, then
    → Print num2 is the largest
5. If num3 >= num1 and num3 >= num2, then
    → Print num3 is the largest
6. End



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.