473,387 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Comparing 3 Integers and Displaying the Largest and smallest

Hello, I am trying to write java program to input three integer numbers compare them and display the largest and Smallest number. I have thought about the problem I would need to use a comparison operate determine which number is larger and which is smaller. I am new to java I was wondering how to but this into java source code.

Can anyone Help?

Thank You.

MBeckfod05
Apr 6 '07 #1
4 47617
JosAH
11,448 Expert 8TB
Just put your three numbers in an array and use the Arrays.sort() method.
The number at the start of the array will be the smallest number; the one at the
end will be the largest number.

kind regards,

Jos
Apr 6 '07 #2
You can also just use if-statements. For 3 integers i would say an array is a bit of an overkill. Just check a couple times which one is the largest and which one is the smallest.

as an example:
Expand|Select|Wrap|Line Numbers
  1. if ( x > y) {
  2. largest = x;
  3. }
  4.  
You will have to do this a couple times and you are all set.

BSCode266
Apr 9 '07 #3
Ganon11
3,652 Expert 2GB
I agree with BSCode. As this sounds like a class assignment type question, it is probably being covered in an intro-level course, and they may not have even gotten to arrays yet!

Using if...statements is probably your best bet. I would use two 'checks' in each statement - for example, I would want to check if a is bigger than b AND if a is bigger than c.
Apr 9 '07 #4
JosAH
11,448 Expert 8TB
I agree with BSCode. As this sounds like a class assignment type question, it is probably being covered in an intro-level course, and they may not have even gotten to arrays yet!

Using if...statements is probably your best bet. I would use two 'checks' in each statement - for example, I would want to check if a is bigger than b AND if a is bigger than c.
Don't do that because you'll be throwing away valuable information then. Write it
out and you'll see that you're basically doing this:
Expand|Select|Wrap|Line Numbers
  1. if (a >= b && a >= c) max= a;
  2. else if (b >= a && b >= c) max = b;
  3. else if (c >= a && c >= b) max= c;
... and then you have to do the same for the minimum value; on top of that
you'll be testing quite a bit of comparisons twice.

Better do this then:
Expand|Select|Wrap|Line Numbers
  1. if (a >= b) 
  2.    if (a >= c) { max= a; if (b >= c) min= c; else min= b; }
  3.    else { max= c; min= b; }
  4. else if (b >= c)
  5.    { max= b; if (a >= c) min= c; else min= a; }
  6. else { max= c; if (a >= b) min= b; else min= a; }
kind regards,

Jos (<--- cycle squeezer ;-)
Apr 9 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Keke922 | last post by:
I have to write a program that allows the user to enter a series of integers and -99 when they want to exit the loop. How do I display the largest and smallest number the user entered?
7
by: Tweaxor | last post by:
I stuck with exerise in a Learning C book that I got. If I have the numbers as input. How can I determine which of the three is the smallest number, largest number and the range. Or would it be...
13
by: Peter Ammon | last post by:
I have a floating point number. I'd like to get the nearest floating point number that is larger or smaller than the given number. I investigated FLT_EPSILON but it only seems to be useful if the...
11
by: gouqizi.lvcha | last post by:
Hi, All: I wonder what is the smallest positive double numbers in C in 32 bit CPU? Rick
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
1
by: RN1 | last post by:
Sometimes I find that though I am comparing 2 integers, the result turns out to be unexpected. For e.g. an ASP page encapsulates recordset paging. <% Dim iPage,iPageCounter ...
8
crystal2005
by: crystal2005 | last post by:
I am writing a program that receive maximum of 25 line of string each has 20 characters maximum. The program will print the smallest and the largest string. However the following program gives me...
5
by: Bernardo | last post by:
Okay, so I am making a program that accepts user input of integers. As you can see I am using an array and i need to get the greatest number, and smallest, range, mean, etc. But I successfully did...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.