Java Program to Find the Largest Among Three Numbers

Java Program to Find the Largest Among Three Numbers

What is Number ?

An item in mathematics used for measurement, counting, and labelling is a number. Many categories can be used to categorise numbers, including natural, whole, integer, rational, irrational, real, and complex numbers. They carry out computations and offer solutions in a number of mathematical operations, including addition, subtraction, multiplication, and division. 

Steps to Find the Largest Among Three Numbers :

  1. Put the three numbers in variables after taking them as input.
  2. In a new variable, let’s call it “temp,” compare the first and second numbers and store the greater one there.
  3. If the third number is greater than the “temp” variable, compare the two numbers and put the higher value in the “temp” variable.
  4. The “temp” variable now has the biggest number of the three. As output, print the value of the variable “temp.”

Pseudo Code for the above algorithm :

Input: three numbers a, b, c

temp = a
if b > temp:
    temp = b
if c > temp:
    temp = c

Output: temp

Example 1 : 

Run
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int a, b, c, largest;

        System.out.print("Enter the first number: ");
        a = input.nextInt();

        System.out.print("Enter the second number: ");
        b = input.nextInt();

        System.out.print("Enter the third number: ");
        c = input.nextInt();

        largest = a;
        if (b > largest) {
            largest = b;
        }
        if (c > largest) {
            largest = c;
        }

        System.out.println("The largest number is " + largest);
    }
}

Output :

Enter the first number: 12
Enter the second number: 17
Enter the third number: 5
The largest number is 17

Example 2 : 

Run
public class Main {
    public static void main(String[] args) {
        int a = 10, b = 20, c = 30, largest;

        if (a > b) {
            if (a > c) {
                largest = a;
            } else {
                largest = c;
            }
        } else {
            if (b > c) {
                largest = b;
            } else {
                largest = c;
            }
        }

        System.out.println("The largest number is " + largest);
    }
}

Output :

The largest number is 30

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription