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

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

 Hey there, welcome back to another post. In this post, we are going to learn how to find the smallest number from the given three numbers in Java.

Java Program to Find Smallest of Three Numbers

We have given three numbers. Write a Java program to return the smallest number.

Example 1: 

Input: [5, 3, 7]
Output: 3

Solution

Using the if-else conditions, we can find the smallest number from the given three numbers.

Java Program: 

/**
 * https://www.thecodingshala.com/
 */

import java.util.*;

public class Main {

    public static int findSmallest(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 smallest = findSmallest(num1, num2, num3);
        System.out.println("Smallest number from " + num1 + ", " + num2 + ", and " + num3 + " is: " + smallest);
    }
}

Output: 

Enter first number
11
Enter second number
65
Enter third number
34
Smallest number from 11, 65, and 34 is: 11


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

Anti Diagonals - The Coding Shala

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

LeetCode - Bulb Switcher Solution - The Coding Shala

New Year Chaos Solution - The Coding Shala

Sorting the Sentence LeetCode Solution - The Coding Shala