Java Program to Find Smallest 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 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
- Java Program to Find Largest of Three Numbers
- Java Program to Print Even Numbers from 1 to N
- Java Program to Check if the Number is a Palindrome
- Java Program to Find the Area of a Triangle
- Java Program to Print Fibonacci Series
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