Results 1 to 20 of 27
 11122011, 08:21 AM #1Member
 Join Date
 Nov 2011
 Posts
 19
 Rep Power
 0
smallest number and largest number using while and if statements
hi i am having some problems on a basic program. i have to ask the user how many numbers they want to input and then based on this i have to find the largest and smallest numbers of the list. i can only use if statements and while statements. i have done the first part but i don’t understand how to begin the other part. my code so far is:
package whilesmalllarge;
import TerminalIO.KeyboardReader;
public class Smalllarge {
public static void main(String[] args) {
KeyboardReader reader = new KeyboardReader();
double usernumber= 0 ;
double i = 0;
double count;
count =reader.readInt(" How long is the list?");
while( i < count){
usernumber = reader.readInt(" input a number:");
i++;}
a test run:
How long is the list?3
input a number:3
input a number:5
input a number:4
Now it just has to say which number is the biggest and which one is the smallest
 11122011, 08:56 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
Re: smallest number and largest number using while and if statements
You're on the rigth track; now you have to fill in the details; now you have something like this:
Java Code:while (i < count) { read the input into usernumber i++; }
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 11122011, 08:59 AM #3Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 15
Re: smallest number and largest number using while and if statements
You can't write the code until you have a "plan of attack". Suppose you were presented with a bunch of numbers one at a time and knew that at the end you were going to be asked which was the smallest. How would you do it?
In particular, what would you have to remember?
In code whatever it is that you had to remember would be a variable, and you would update it each time around the while loop.
[Edit] slow... ;(
 11122011, 09:09 AM #4Member
 Join Date
 Nov 2011
 Posts
 19
 Rep Power
 0
Re: smallest number and largest number using while and if statements
sorry my teacher never said how you should do this. i was in cp intro to programming and then i went into java honors.i have looked at a couple of pos so would i like insert
highest = Integer.MAX_VALUE;
lowest = Integer.MIN_VALUE;
into the while statement?
 11122011, 09:14 AM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
Re: smallest number and largest number using while and if statements
Build a wall around Donald Trump; I'll pay for it.
 11122011, 09:19 AM #6Member
 Join Date
 Nov 2011
 Posts
 19
 Rep Power
 0
 11122011, 10:21 AM #7AlphaSupport Guest
Re: smallest number and largest number using while and if statements
Just forget about programming for a minute.
Imagine you are the "program".
You are told that in the end of the task you will need to tell what was the lowest and the highest number.
Someone will hand you over X numbers, 1 by 1.
Now how are you going to do this?
Remember all the numbers (maybe hundreds) so in the end you can look which was the highest and which the lowest?
What steps do you need to do?
Answer this in real life and you can "translate" it into software.
 11122011, 05:39 PM #8Member
 Join Date
 Nov 2011
 Posts
 19
 Rep Power
 0
Re: smallest number and largest number using while and if statements
yes i think i understand what to do but i dont know how to do it. i know i have to set a max and a min and this is where the first 2 numbers will go. then using the if statements i have to keep updating it.
 11122011, 06:58 PM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
Re: smallest number and largest number using while and if statements
Don't even bother about the first two numbers; there is one special situation: you don't have any numbers yet. That notion begs for a boolean value; part of the code might look like this:
Java Code:if (noNumbersYet) min= currentNumber; else if (currentNumber < min) min= currentNumber; noNumbersYet= false;
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 11122011, 07:25 PM #10Member
 Join Date
 Jul 2011
 Location
 New Delhi,India
 Posts
 56
 Rep Power
 0
Re: smallest number and largest number using while and if statements
you can use this algo if you want to
int lar=99999,small=999999,i=0;
String s;
int num=0;
while(i<count){//count=no. of numerals entered
System.out.println("Enter the number:");
try{
s=br.readLine();
num=Integer.parseInt(s);
}catch(IOException ioe){
System.out.println(ioe);
}
if(num>lar){
lar=num;
}
if(num<small){
small=num;
}
i++;
}
System.out.println("Largest:"+lar+"\nSmallest:"+sm all);
 11122011, 08:57 PM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
Re: smallest number and largest number using while and if statements
Cool, now suppose the largest number in the sequence of numbers is < 99999 or the smallest number > 99999 ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 11122011, 09:02 PM #12Member
 Join Date
 Jul 2011
 Location
 New Delhi,India
 Posts
 56
 Rep Power
 0
 11122011, 09:05 PM #13
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25

Re: smallest number and largest number using while and if statements
 11122011, 10:54 PM #15Member
 Join Date
 Jul 2011
 Location
 New Delhi,India
 Posts
 56
 Rep Power
 0
Re: smallest number and largest number using while and if statements
but i think its a better algorithm then storing all the elements of array and then searching.....i think we can set the variables as double and assign them value of infinity....can we????

Re: smallest number and largest number using while and if statements
 11122011, 11:03 PM #17Member
 Join Date
 Jul 2011
 Location
 New Delhi,India
 Posts
 56
 Rep Power
 0
Re: smallest number and largest number using while and if statements
it actually worked.....just change the line containing
int lar=99999,small=99999 with
double lar=1/0.,small=1/0.;
its working fine.....
 11132011, 01:47 AM #18Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 15
Re: smallest number and largest number using while and if statements
Java Code:double lar=1/0.,small=1/0.;
[Edit] Jos and Fubarable, above, have given standard answers to what is a standard problem. I don't think there's any value to the less flexible approach of dividing by zero.
 11132011, 03:25 AM #19Member
 Join Date
 Nov 2011
 Posts
 19
 Rep Power
 0
Re: smallest number and largest number using while and if statements
ok i understand what you are saying josh but what would the noNumbersYet variable be?
 11132011, 09:03 AM #20
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
Similar Threads

Find the Biggest Number + Smallest
By d4v1dz in forum New To JavaReplies: 19Last Post: 10262011, 06:43 AM 
How can I find out the largest number ... ?
By TheTypicalStudent in forum New To JavaReplies: 14Last Post: 05032011, 11:09 AM 
Find the second largest number in the array
By radhi16 in forum New To JavaReplies: 5Last Post: 01132011, 06:08 PM 
smallest number java program
By elmo in forum New To JavaReplies: 9Last Post: 11182010, 10:29 PM 
Finding the largest number in an array
By starchildren3317 in forum New To JavaReplies: 14Last Post: 11032010, 07:49 AM
Bookmarks