Java Program to Find Largest of Three Numbers - The Coding Shala

Home >> Java Programs >> Find Largest of Three Numbers

 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
Please leave a comment below if you like this post or found some errors, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Add two numbers in Scala - The Coding Shala

LeetCode - Number of Good Pairs Solution - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala