Java Program to Find Largest of Three Numbers - The Coding Shala
Hey there, welcome back to another post. In this post, we are going to learn how to find the largest number from the given three numbers in Java.
Java Program to Find Largest of Three Numbers
We have given three numbers. Find the Largest Number using Java Program.
Example 1:
Input: 3, 2, 5
Output: 5
Solution
Using the if-else conditions we can find the largest number from the given three numbers.
Java Program:
/** * https://www.thecodingshala.com/ */ import java.util.*; public class Main { public static int findLargest(int num1, int num2, int num3) { if (num1 >= num2) { if (num1 >= num3) { return num1; } else { return num3; } } else { if (num2 >= num3) { return num2; } else { return num3; } } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter first number"); int num1 = sc.nextInt(); System.out.println("Enter second number"); int num2 = sc.nextInt(); System.out.println("Enter third number"); int num3 = sc.nextInt(); int largest = findLargest(num1, num2, num3); System.out.println("Largest number from " + num1 + ", " + num2 + ", and " + num3 + " is: " + largest); } }
Output:
Enter first number 11 Enter second number 5 Enter third number 14 Largest number from 11, 5, and 14 is: 14
Other Posts You May Like
- Java Program to Check Armstrong Number
- Java Program to Check if a Given Number is Perfect Square
- Java Program to Check Prime Number
- Java Program to Check if a String is a Palindrome
- Java Program to Swap Two Numbers
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.
Comments
Post a Comment