

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I wrote a program that calculate the smallest number. But I don't know how programers would do it. I did it by "IF statement", which is working, but not sure if it is the standard or common way of coding it.

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner in = new Scanner(;
    System.out.print("Enter three values: ");

    int num1 = in.nextInt();
    int num2 = in.nextInt();
    int num3 = in.nextInt();

    System.out.print(smallest(num1, num2, num3));


public static int smallest(int num1, int num2, int num3)
    if (num1 < num2 && num1 < num3){return num1;}
    else if (num2 < num1 && num2 < num3){return num2;}
    else return num3;
share|improve this question
In one word - No. It can be done a lot more simply and elegantly. – Stephen C Jul 6 '13 at 18:11

There is Math#min method. You can use that one:

minimum = min(n1, min(n2, n3))
share|improve this answer
You need Math.min as you can't drop the class for static methods. – Zong Zheng Li Jul 6 '13 at 18:36
You can with import static :) – Ayman Jul 6 '13 at 18:39
Did not know that. Awesome. – Zong Zheng Li Jul 6 '13 at 18:41
Arguably the OP's version is simpler/more efficient if you consider the complexity hidden in min. – Hot Licks Jul 6 '13 at 19:20

Another idea would be to not even store all of the numbers but instead just keep track of the smallest, if that's all you're looking for:

int smallest = in.nextInt();

for (int i = 0; i < 2; i++) {  // take 2 more inputs
    int next = in.nextInt();
    if (next < smallest)
        smallest = next;

If you indeed do not need to access inputs other than the smallest later on, this approach would likely be optimal.

share|improve this answer
This only works for three numbers though, it would be better to design a function that will take an arbitrarily amount of numbers. – skiwi Jul 6 '13 at 20:22
@skiwi All you need to do is change that 2 (and maybe wrap a function around the code snippet); that's not hard - the concept remains the same. – arshajii Jul 6 '13 at 20:27

You can solve the more general problem of finding the smallest value of an array (or a list). It is a bad idea to sort() the structure, as you only need to find the smallest element. A really basic technique for doing so would be something like that:

public int smallest(int[] array) {
    if(array.length == 0) throw new IllegalArgumentException();  
    int min = array[0];
       if(array[i] < min)
          min = array[i];
    return min;

This has a O(n) complexity which is minimal for a non-sorted array because you have to go through the entire array anyway.

This is of course only optimal in the general case of an array that's already full of number. If you only need to get the minimum from user's inputs then you should definitely go for arshajii's algorithm to save a bit of memory because it allows you not to store the entire array.

share|improve this answer

I would do this:

public static int smallest(int... nums) {
    return nums[0];

Not only is it minimal elegant code, by using a varargs parameter, it can handle any quantity of ints.

And the code that calls it need not be altered.

share|improve this answer
@stephenc you mean like this? :) – Bohemian Jul 6 '13 at 20:52
This is similar to skiwi's answer. The varargs idea is a good one, but why add the extra complexity of sorting when a simple and more efficient linear traversal of the array can be done just as easily? Sure, it makes the code look more "elegant", but that's a trade-off that's rarely worth it. – arshajii Jul 7 '13 at 15:37
@arshajii the most expensive commodity is programmer time, not CPU time. The performance difference between this code and some if statements would be measured in nanoseconds. Yes, it would be slower, but a) this code means no one else has to write code when more values come along, and b) 2 lines of code are written and the coder gets on with something else. Unless the usage is extremely performance critical (very rare), it's not going to make a measurable difference. Premature optimisation is the root of all evil - Donald Knuth. – Bohemian Jul 7 '13 at 20:31

A way of doing this is by creating an array of integers, and then sorting it and grabbing the first element.

Something like:

int[] input = new int[]{in.nextInt(), in.nextInt(), in.nextInt()};
int min = input[0];

Also seeing as you have made a function for it, you could turn that one into the following, instead of my above approach:

public static int smallest(int... numbers) {
    if (numbers.length == 0) {
        throw new IllegalArgumentException("numbers: numbers.length == 0");
    return numbers[0];

In this example you are using varargs, meaning that you can put in as many ints as you want. The varargs argument is essentially an array once it gets into your function, so then you can just work with it like any array. Be sure to do a check on the number of items in the array though, as varargs can also be 0.

You can call the code the same way as the old one:

int smallest = smallest(5, 10, 15); will return 5.

share|improve this answer
Sorting is O(nlog(n)); it is much simpler to find the smallest number in O(n) time by just traversing the array. – arshajii Jul 6 '13 at 18:50
@arshajii It has nowhere been mentioned that time is an issue. It could be a nice sidenote to my story though I see no reason for a downvote for that. It is only to be considered when you are going to sort a big array of numbers. – skiwi Jul 6 '13 at 20:21
It doesn't matter if time is an issue or not: what possible advantage does sorting have over the one-time traversal method in this scenario? – arshajii Jul 6 '13 at 20:29

Your Answer


