12,251,293 members (52,648 online)
Technical Blog
alternative version

5K views
2 bookmarked
Posted

# How to find the largest of 3 numbers

, 16 Apr 2014 CPOL
 Rate this:
Wait, if you are like man, what the hell? This is a silly program and you&#8217;re like we all have done it in our college or school. This has something more to it and you might find it interesting, so read on. Now, most of us used to write some version of this: Can this [&#8230;]The post How to fin

Wait, if you are like man, what the hell? This is a silly program and you’re like we all have done it in our college or school. This has something more to it and you might find it interesting, so read on.

Now, most of us used to write some version of this:

```public class FindLargest{

public static void main(String[] args){

int a = 10, b = 20, c = 30;

if(a >= b && a>=c){
System.out.println("Largest number is : " + a);
}
if(b >= a && b>=c){
System.out.println("Largest number is : " + b);
}
if(c >= a && c>=b){
System.out.println("Largest number is : " + c);
}

}
}```

Can this solution be improved? Yes. How?

```public class FindLargest{
public static void main(String[] args){
int a = 10, b = 20, c = 30;
int largest = findLargest(a, b, c);
System.out.println("The largest number is : " + largest);
}
public static int findLargest(int a, int b, int c){
int largest = a; // assume the first value is largest
if(b > largest){
largest = b; // see if b is the largest
}
if(c > largest){
largest = c;  // see if c is the largest
}
return largest;
}
}```

What is nice about above code is it’s neat and easy to understand. Rather than just blindly checking each number is largest than the other two, we assume first number to be the largest and assign it to a variable called ‘largest’. Then we check if other number is larger than the ‘largest’. If it is, then we assign that number to variable ‘largest’. We do this for each number. So at the end of all this comparisons we should have the largest number in our ‘largest’ variable and we would just return it! Another thing to note here is that we have reduced number of comparisons by 1. Also see that this code is easy to modify for 4 or more numbers.

Now, if you are like ‘dude, isn’t there a way to write this code so that I don’t have to modify it if I change my mind later and want it to work with n numbers?!!’ fortunately there is.’

So, what is the solution?
Yes, the answer is varargs. And here is the code.

```public class FindLargest{
public static void main(String[] args){
int a = 10, b = 20, c = 30, d = 50;
int x = 5;
int p = 9, q = 3;

System.out.println("The largest number is : " + findLargest(a, b, c, d));

System.out.println("The largest number is : " + findLargest(x));

System.out.println("The largest number is : " + findLargest(p, q));

}
public static int findLargest(int a, int... nums){
int largest = a; // assume the first value is largest
for(int num : nums){
if(num > largest){
largest = num;
}
}
return largest;
}
}```

You can find these codes in java and ruby here
I hope you find this post helpful. And if you have any doubts or suggestions feel free to drop a comment.

The post How to find the largest of 3 numbers appeared first on Bhargav's Blog.

## Share

 Software Developer (Junior) India
Hi, I’m Bhargav Kaneria. I live in Pune, India. By profession I am a young software
developer(Trainee). I like to learn new languages(obviously programming!). I’m comfortable in Java. I also have some knowledge of Ruby, Groovy and a bit of JavaScript(learning currently) and a bit of PHP. My area of interests include programming(duh!), Internet, data structures and algorithms, economics and history(mostly related to world war II).

## You may also be interested in...

 First Prev Next
 LINQ Jörg Reinhardt23-May-14 2:44 Jörg Reinhardt 23-May-14 2:44
 Sorry for being facetious... but terjeber23-Apr-14 23:07 terjeber 23-Apr-14 23:07
 Re: Sorry for being facetious... but Bhargav Kaneria24-Apr-14 1:05 Bhargav Kaneria 24-Apr-14 1:05
 Re: Sorry for being facetious... but terjeber25-Apr-14 1:25 terjeber 25-Apr-14 1:25
 Can this be done with an IEnumerable extension method? George Swan16-Apr-14 20:42 George Swan 16-Apr-14 20:42
 Re: Can this be done with an IEnumerable extension method? Bhargav Kaneria17-Apr-14 7:50 Bhargav Kaneria 17-Apr-14 7:50
 Last Visit: 31-Dec-99 18:00     Last Update: 4-May-16 14:33 Refresh 1