Find Largest Among Three Numbers in Java.

find greatest among three numbers in java

In this article, we are going to learn Java programs to find the greatest number among three integer numbers. There are multiple ways to solve this program and we will each of them one by one.


Approach 1: Find the Greatest using if-else statement.

In this approach, we will use if-else statements to compare the three numbers and find the greatest among them.

Java Example Code:

import java.util.Scanner;

public class GreatestOfThreeNumbers {

   public static void main(String[] args) {

      int num1, num2, num3;
      Scanner input = new Scanner(System.in);

      System.out.println("Enter three numbers:");
      num1 = input.nextInt();
      num2 = input.nextInt();
      num3 = input.nextInt();

      if (num1 > num2 && num1 > num3) {
         System.out.println(num1 + " is the greatest.");
      }
      else if (num2 > num1 && num2 > num3) {
         System.out.println(num2 + " is the greatest.");
      }
      else {
         System.out.println(num3 + " is the greatest.");
      }
   }
}
Output:
Enter three numbers:
12
23
11
23 is the greatest.

Here, we are taking input of three numbers from the user using the Scanner class. Then, we are using if-else statements to compare the numbers and print the greatest among them.


Approach 2: Using Math.max() method.

In this approach, we will use the Math.max() method to find the greatest among the three numbers.

Java Example Code:
import java.util.Scanner;

public class GreatestOfThreeNumbers {

   public static void main(String[] args) {

      int num1, num2, num3;
      Scanner input = new Scanner(System.in);

      System.out.println("Enter three numbers:");
      num1 = input.nextInt();
      num2 = input.nextInt();
      num3 = input.nextInt();

      int greatest = Math.max(num1, Math.max(num2, num3));
      System.out.println(greatest + " is the greatest.");
   }
}
Output:
Enter three numbers:
10
23
15
23 is the greatest.

Here, we are using the Math.max() method to find the greatest among the three numbers. We are taking input of three numbers from the user using the Scanner class and then passing them as arguments to the Math.max() method. The Math.max() method returns the greatest of the two numbers passed as arguments, so we are calling it twice to compare all three numbers.

Approach 3: Using Array and Loops.

In this approach, we will use arrays and loops to find the greatest among the three numbers.

Java Example Code:
//Java code for finding greatest number
import java.util.Scanner;

public class GreatestOfThreeNumbers {

   public static void main(String[] args) {

      int[] nums = new int[3];
      Scanner input = new Scanner(System.in);

      System.out.println("Enter three numbers:");
      for (int i = 0; i < 3; i++) {
         nums[i] = input.nextInt();
      }

      int greatest = nums[0];
      for (int i = 1; i < 3; i++) {
         if (nums[i] > greatest) {
            greatest = nums[i];
         }
      }
      System.out.println(greatest + " is the greatest.");
   }
}
Output:
Enter three numbers:
19
29
15
29 is the greatest.

Here, we are using an array to store the three numbers entered by the user. We are taking input of three numbers from the user using the Scanner class and then using a loop to store them in the array. Then, we are using another loop to compare the numbers in the array and find the greatest among them.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS