Java – Find Smallest of Three Numbers

In this tutorial, we shall learn how to find the smallest of three numbers using different approaches.

You can find smallest of three numbers using if-else statement, if-else-if ladder or ternary operator. We shall go through each of these with example programs.

Example 1 – Find Smallest of Three Numbers using If-Else

In this example, we shall use the following algorithm to find the smallest of three numbers. We shall realize this algorithm using Java If-Else statement.

Algorithm

  1. Start.
  2. Take three numbers in a, b, c.
  3. Check if a is less than b.
  4. If above condition is true, go to step 5, else go to step 7.
  5. Check if c is less than a.
  6. If above condition is true, c is the smallest, else a is the smallest. Go to step 9.
  7. Check if b is less than c.
  8. If above condition is true, b is the smallest, else c is the smallest.
  9. Stop.

Java Program

</>
Copy
/**
 * Java Program - Find Smallest of Three Numbers
 */

public class SmallestInThree {

	public static void main(String[] args) {
		//three numbers
		int a = 20;
		int b = 10;
		int c = 30;
		
		int smallest;
		
		//find the smallest
		if(a<b) {
			if(c<a) {
				smallest = c;
			} else {
				smallest = a;
			}
		} else {
			if(b<c) {
				smallest = b;
			} else {
				smallest = c;
			}
		}
		
		System.out.println(smallest + " is the smallest.");
	}
}

Run the above program and we shall get the smallest of three numbers as shown in the following console output.

10 is the smallest.

Example 2 – Find Smallest of Three Numbers using If-Else-If Ladder

In this example, we shall use the following algorithm to find the smallest of three numbers. We shall implement the following algorithm using Java if-else-if statement.

Algorithm

  1. Start.
  2. Take three numbers in a, b, c.
  3. Check if a is less than b and a is less than c.
  4. If above condition is true, a is smallest and go to step 7, else go to step 5.
  5. Check if b is less than c.
  6. If above condition is true, b is the smallest, else c is the smallest.
  7. Stop.

Java Program

</>
Copy
/**
 * Java Program - Find Smallest of Three Numbers
 */

public class SmallestInThree {

	public static void main(String[] args) {
		//three numbers
		int a = 20;
		int b = 10;
		int c = 30;
		
		int smallest;
		
		if(a<b && a<c) {
			smallest = a;
		} else if (b<c) {
			smallest = b;
		} else {
			smallest = c;
		}
		
		System.out.println(smallest + " is the smallest.");
	}
}

Runt he above program and we shall get that 10 is the smallest of the taken three numbers.

10 is the smallest.

Example 3 – Find Smallest of Three Numbers using Ternary Operator

The algorithm for this example, is kind of same as that of the first example, where we used if-else statement. But, in this example, we use nested ternary operator to crunch all the code to a single line.

Java Program

</>
Copy
/**
 * Java Program - Find Smallest of Three Numbers
 */

public class SmallestInThree {

	public static void main(String[] args) {
		//three numbers
		int a = 20;
		int b = 10;
		int c = 30;
		
		int smallest = (a<b)?(a<c?a:c):(b<c?b:c);
		
		System.out.println(smallest + " is the smallest.");
	}
}

Run the above program and you shall get the following output. You may change the values for a, b and c and check the result.

10 is the smallest.

Conclusion

In this Java Tutorial, we learned how to find the largest of three given numbers, using different conditional expressions in Java programming language.