Java Program to Find Largest of Three Numbers

In this tutorial, we will develop a program to find the greatest of three numbers in Java. There are many different ways to find the greatest of three numbers we will see them one by one.

Largest of three numbers in Java using if else

import java.util.Scanner;

public class LargestNumber {

    public static void main(String[] args) {

       // declare variables
       double num1 = 0.0, num2 = 0.0, num3 = 0.0;
       double largestNumber = 0.0;

       // create Scanner class object
       Scanner scan = new Scanner(System.in);

       // read inputs
       System.out.print("Enter three numbers:: ");
       num1 = scan.nextDouble();
       num2 = scan.nextDouble();
       num3 = scan.nextDouble();

       // find largest number
       if(num1 >= num2) {
            if(num1 >= num3) largestNumber = num1;
            else largestNumber = num3;
       } else {
            if(num2>=num3) largestNumber = num2;
            else largestNumber = num3;
       }

       // display result
       System.out.println("Largest number = "+ largestNumber);

       // close Scanner class object
       scan.close();

     }
 }

Output for different testcases:-

Enter three numbers:: 15 5 25
Largest number = 25.0

Enter three numbers:: 50 10 40
Largest number = 50.0

Enter three numbers:: 55 95.51 48
Largest number = 95.51

In this program, we have written the logic inside the main method, but it is the wrong way. It was the only to understanding purpose. We should develop a program in the way that the logic will be in the user-defined method and we should call them from the main method. The below-given program demonstrates it.

import java.util.Scanner;

public class LargestNumber {

   public static double findLargest(double a, double b, double c) {
         if(a >= b) {
            if(a >= c) return a;
            else return c;
         } else {
            if(b>=c) return b;
            else return c;
         }
   }

   public static void main(String[] args) {

       // declare variables
       double num1 = 0.0, num2 = 0.0, num3 = 0.0;
       double largestNumber = 0.0;

       // create Scanner class object
       Scanner scan = new Scanner(System.in);

       // read inputs
       System.out.print("Enter three numbers:: ");
       num1 = scan.nextDouble();
       num2 = scan.nextDouble();
       num3 = scan.nextDouble();

       // find largest number
       largestNumber = findLargest(num1, num2, num3);

       // display result
       System.out.println("Largest number = "+ largestNumber);

       // close Scanner class object
       scan.close();

     }
 }

Using && operator and if-else

Previously we developed if-else statements inside another if-else statement. We can write it in another way using and (&&) operator.

public static double findLargest(double a, double b, double c) {
     if(a >= b && a >=c ) return a;
     else if(b >= a && b >= c) return b;
     else return c;
}

In the three numbers num1, num2, and num3, if the num1 is greater than the remaining two numbers than num1 is the largest number, else if the num2 is greater than the remaining two numbers than num2 is the largest number, else the num3 is the largest number.

import java.util.Scanner;

public class LargestNumber {

   public static double findLargest(double a, double b, double c) {
       if(a >= b && a >=c ) return a;
       else if(b >= a && b >= c) return b;
       else return c;
   }

   public static void main(String[] args) {

       // declare variables
       double num1 = 0.0, num2 = 0.0, num3 = 0.0;
       double largestNumber = 0.0;

       // create Scanner class object
       Scanner scan = new Scanner(System.in);

       // read inputs
       System.out.print("Enter three numbers:: ");
       num1 = scan.nextDouble();
       num2 = scan.nextDouble();
       num3 = scan.nextDouble();

       // find largest number
       largestNumber = findLargest(num1, num2, num3);

       // display result
       System.out.println("Largest number = "+ largestNumber);

       // close Scanner class object
       scan.close();

     }
 }

Largest of three numbers in Java using if

public static double findLargest(double a, double b, double c) {
   double largestNumber = a;
   if(largestNumber <= b) largestNumber = b;
   if(largestNumber <= c) largestNumber = c;
   return largestNumber;
}

In this method, the first number is assigned to a local variable. Now, if the second number is larger than the local variable then the second number may be the largest number among three numbers so, assign the local variable with the second number.

Now, the comparison is remaining between the local variable and the third number. If the third number is greater than the local variable then it is the largest number else local variable is the largest number.

import java.util.Scanner;

public class LargestNumber {

   public static double findLargest(double a, double b, double c) {
       double largestNumber = a;
       if(largestNumber <= b) largestNumber = b;
       if(largestNumber <= c) largestNumber = c;
       return largestNumber;
   }

   public static void main(String[] args) {

       // declare variables
       double num1 = 0.0, num2 = 0.0, num3 = 0.0;
       double largestNumber = 0.0;

       // create Scanner class object
       Scanner scan = new Scanner(System.in);

       // read inputs
       System.out.print("Enter three numbers:: ");
       num1 = scan.nextDouble();
       num2 = scan.nextDouble();
       num3 = scan.nextDouble();

       // find largest number
       largestNumber = findLargest(num1, num2, num3);

       // display result
       System.out.println("Largest number = "+ largestNumber);

       // close Scanner class object
       scan.close();

     }
 }

Greatest of three numbers in Java using conditional operator

public static double findLargest(double a, double b, double c) {
   return (a>b && a>c)? a: (b>a && b>c)? b:c;
}

In this method, we use a conditional operator. In the numbers, a, b and c if the a is greater than b and c then return a else compare b with a and c. If the b is greater than a and c then return b else return c.

import java.util.Scanner;

public class LargestNumber {

   public static double findLargest(double a, double b, double c) {
       return (a>b && a>c)? a: (b>a && b>c)? b:c;
   }

   public static void main(String[] args) {

       // declare variables
       double num1 = 0.0, num2 = 0.0, num3 = 0.0;
       double largestNumber = 0.0;

       // create Scanner class object
       Scanner scan = new Scanner(System.in);

       // read inputs
       System.out.print("Enter three numbers:: ");
       num1 = scan.nextDouble();
       num2 = scan.nextDouble();
       num3 = scan.nextDouble();

       // find largest number
       largestNumber = findLargest(num1, num2, num3);

       // display result
       System.out.println("Largest number = "+ 
                               largestNumber);

       // close Scanner class object
       scan.close();

     }
 }

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *