473,569 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing 3 Integers and Displaying the Largest and smallest

7 New Member
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 47663
JosAH
11,448 Recognized Expert MVP
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
BSCode266
38 New Member
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 Recognized Expert Specialist
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 Recognized Expert MVP
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
8879
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
6698
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 simpler to just find the range of the three numbers. Thanks in advance :D
13
5128
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 given number is 1. Any suggestions? Thanks, -Peter
11
13103
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
3373
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 checkInteger(&$value, $checks) { $err = ''; if (!is_numeric($value) || (floatval($value) != intval($value))) { $err .= 'Input must be an integer. ';
1
4359
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 iPage=Request.QueryString("Page") If(iPage="") Then
8
12165
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 Segmentation fault (core dumped) :(( It looks simple but i have no idea what went wrong.... Can anyone help me out?? #include<stdio.h>...
5
4913
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 this for the greatest and figured not much would need to be changed but everything I've tried has given me errors of like -8732113 as the smallest. If...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.