Skip to main content

Command Palette

Search for a command to run...

Java Programming : Greater of three numbers

Updated
2 min readView as Markdown
Java Programming : Greater of three numbers

Example

Input
enter the first number 3
enter the second number 5
enter the third number 4 
Output
The largest number is 5

Required to Know

Use of Variables, Use of different operators like assignment or arithmetic operators, Use of methods like System. out.print(), Use of If-else, and Use of Data types.

Steps

  • Take three numbers as input from the user.

  • Check if the first_number is greater than the second_number and third_ number, and print the first_number.

  • Check if the second_number is greater than the third_number, and print the second_number. Otherwise, print third_number.

Condition

if(first_number>second_number>third_number){
    print(first_number)
}
elif(second_number>third_number){
    print(second_number)
}
else{
    print(third_number)
}

Program

import java.util.Scanner;

public class greater {

    /**
     * This class compares three numbers and prints the largest number.
     */

    public static void main(String[] args) {

        // Create a Scanner object to read user input.
        Scanner sc = new Scanner(System.in);

        // Prompt the user to enter three numbers.
        System.out.print("Enter three numbers: ");
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();

        // Check if a is greater than b and c.
        if (a > b && a > c) {

            // Print a.
            System.out.println("The largest number is " + a);

        } else if (b > c) {

            // Print b.
            System.out.println("The largest number is " + b);

        } else {

            // Print c.
            System.out.println("The largest number is " + c);

        }

    }
}

More from this blog

TheCodingcoder

60 posts

Empowering the coding community by simplifying complex tasks.