Java Program to find smallest of three numbers


April 13, 2022, Learn eTutorial
2070

For implementing a java program to find out the smallest of three numbers, we have to read three integer numbers from the user, and by using an if-else statement we can find out the lowest number.

How find the smallest of three numbers using the Java program?

First, we have to declare the class SmallestNumber. Now we have to declare the variables 'a', 'b', 'c' to hold the three integer numbers. Then read the numbers from the user using the scanner class object. By using if condition check if 'a' is less than 'b' and 'c', then print the smallest as 'a'. else check 'b' is less than 'c', if so print 'b' is the lowest. finally, if none of these conditions are met, then print 'c'.

ALGORITHM

STEP 1: Declare the class Smallest as public.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Declare the integer variables a,b,c.

STEP 4: Create the object of the scanner class as sc.

STEP 5: Read the three numbers into the variables a,b,c.

STEP 6: Check if a

STEP 7: Print the smallest number is a

STEP 7: Else check if b< c, if true then display the smallest number as b.

STEP 8: Else display the smallest number is c.


 

Java Source Code

                                          import java.util.Scanner;
public class SmallestNumber 
{
    public static void main(String[] args) 
    {
        int a,b,c;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the first number:");
        a = sc.nextInt();
        System.out.println("Enter the second number:");
        b = sc.nextInt();
        System.out.println("Enter the third number:");
        c = sc.nextInt();
        if(a < b && a < c)
        {
            System.out.println("The smallest number is:"+a);
        }
        else if(b < c)
        {
            System.out.println("The smallest number is:"+b);
        }
        else
        {
            System.out.println(" The smallest number is:"+c);
        }
 
    }
}
                                      

OUTPUT

Enter the first number:100
Enter the second number:105
Enter the third number:1
The smallest number is:1