# Finding the largest number

Page 1 of 1

## 13 Replies - 5023 Views - Last Post: 12 June 2011 - 07:24 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=234963&amp;s=5904058b30f35c75c51e7cf96c82dd27&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 pumba

Reputation: 0
• Posts: 28
• Joined: 08-June 11

# Finding the largest number

Posted 08 June 2011 - 03:33 AM

Hi Guys,

First time poster, I hope I don't do any "faux-pas" ... I am a beginner in Java Programming with zero previous programming experience, I think this is why I keep messing up sometimes.

Anyway, I am doing my 4th assignment at the moment, and this is where I am getting lost somehow, basically I have to enter the 10 sales numbers, and find the largest one of them ... the assignment is asking to set a counter of 10 , number and the largest value. I couldn't find anything that actually has the "largest" as a string, so I gave it my best shot below:

```
//class largest

import java.util.Scanner; // program uses class Scanner

public class Largest

{

public static void main ( String args[] )
{

int number; //sets the number as integer.
int sales; //sets sales as integer.
int largest;

int counter = 1;
{

Scanner input = new Scanner (System.in ) ; //sets Scanner

System.out.print (" Please enter 1st sales figure: " );
int	number1 = input.nextInt ();

System.out.printf (" Please enter 2nd sales figure: " );
int	number2 = input.nextInt ();

System.out.printf (" Please enter 3rd sales figure: " );
int	number3 = input.nextInt ();

System.out.printf (" Please enter 4th sales figure: " );
int	number4 = input.nextInt ();

System.out.printf (" Please enter 5th sales figure: " );
int	number5 = input.nextInt ();

System.out.printf (" Please enter 6th sales figure: " );
int	number6 = input.nextInt ();

System.out.printf (" Please enter 7th sales figure: " );
int	number7 = input.nextInt ();

System.out.printf (" Please enter 8th sales figure: " );
int	number8 = input.nextInt ();

System.out.printf (" Please enter 9th sales figure: " );
int	number9 = input.nextInt ();

System.out.printf (" Please enter 10th sales figure: " );
int	number10 = input.nextInt ();

while (counter <= 10);
{
if (number1 > number2);
if (number1       > number3 );
if (number1   	> number4);
if (number1		> number5);
if (number1		>number6);
if (number1	>number7);
if	(number1	>number8);
if	(number1	>number9);
if	(number1	>number10);
largest = number1;
{

if (number2 > number1);
if (number2       > number3 );
if (number2   	> number4);
if (number2		> number5);
if (number2		>number6);
if	(number2	>number7);
if	(number2	>number8);
if	(number2	>number9);
if	(number2	>number10);
largest = number2 ;}

{

if (number3 > number1);
if (number3  > number2 );
if (number3 	> number4);
if (number3  > number5);
if (number3	> number6);
if (number3	> number7);
if (number3	> number8);
if (number3	> number9);
if (number3	> number10);
largest = number3 ;}

{

if (number4 > number1);
if (number4  > number2 );
if (number4 	> number3);
if (number4  > number5);
if (number4	> number6);
if (number4	> number7);
if (number4	> number8);
if (number4	> number9);
if (number4	> number10);

largest = number4 ;}

{

if (number5 > number1);
if (number5  > number2 );
if (number5 	> number3);
if (number5  > number4);
if (number5	> number6);
if (number5	> number7);
if (number5	> number8);
if (number5	> number9);
if (number5	> number10);

largest = number5 ;}

{

if (number6 > number1);
if (number6  > number2 );
if (number6 	> number3);
if (number6  > number4);
if (number6	> number5);
if (number6	> number7);
if (number6	> number8);
if (number6	> number9);
if (number6	> number10);

largest = number6 ;}

{
if (number7 > number1);
if (number7  > number2 );
if (number7 	> number3);
if (number7  > number4);
if (number7	> number5);
if (number7	> number6);
if (number7	> number8);
if (number7	> number9);
if (number7	> number10);

largest = number7 ;
}

{
if (number8 > number1);
if (number8  > number2 );
if (number8 	> number3);
if (number8  > number4);
if (number8	> number5);
if (number8	> number6);
if (number8	> number7);
if (number8	> number9);
if (number8	> number10);

largest = number8 ;
}

{
if (number9 > number1);
if (number9  > number2 );
if (number9 	> number3);
if (number9  > number4);
if (number9	> number5);
if (number9	> number6);
if (number9	> number7);
if (number9	> number8);
if (number9	> number10);

largest = number9 ;
}

{
if (number10 > number1);
if (number10  > number2 );
if (number10 	> number3);
if (number10  > number4);
if (number10	> number5);
if (number10	> number6);
if (number10	> number7);
if (number10	> number8);
if (number10	> number9);

largest = number10 ;
}

System.out.printf ("The Largest number is %d/n" , largest );
}

}
}

}

```

Just to make things clear, and in case I may have misread or overlooked a requirement in the assignment, I will paste it :
" The process of finding the largest value (i.e., the maximum of a group of values) is used frequently
in computer applications. For example, a program that determines the winner of a sales contest
would input the number of units sold by each salesperson. The salesperson who sells the most
units wins the contest. Write a pseudocode program and then a Java application that inputs a series
of 10 integers and determines and prints the largest integer. Your program should use at least the
following three variables:
a) counter: A counter to count to 10 (i.e., to keep track of how many numbers have been
input and to determine when all 10 numbers have been processed).
number: The integer most recently input by the user.
c) largest: The largest number found so far."

Please don't take this as me asking for someone to do my homework for me, I just feel like reaching a dead end in here, and it's a self study thing, so I don't have any classmates to ask...

Is This A Good Question/Topic? 0

## Replies To: Finding the largest number

### #2 xor-logic

• HAL9000 was an Apple product

Reputation: 128
• Posts: 764
• Joined: 04-February 10

## Re: Finding the largest number

Posted 08 June 2011 - 04:01 AM

Your program only needs to output the largest number, so here's what I would do.

At the beginning, initialize the "largest" variable to -1.

Get the user input ten times. Every time they input a number, compare it to largest. If it's biggest, assign that value to largest (the first one will be by default). If it's not, go straight to the next input. It doesn't say you need to store the numbers aside from the largest one.
```//Very-pseudo code
Largest = 0
do 10 times {
number = user input
number larger than Largest? {
Largest = number
}
}
Print "The largest number you put in was " + Largest

```

This post has been edited by xor-logic: 08 June 2011 - 09:08 AM

### #3 pumba

Reputation: 0
• Posts: 28
• Joined: 08-June 11

## Re: Finding the largest number

Posted 08 June 2011 - 04:41 AM

Thank you , that made things much simpler... but I am afraid it made it too simple (or at least i simplified my java program too much). For some reason I ended up with no counter... and I cannot seem to find a way to insert the counter in my code .. My instructor is expecting me to have 1 class and 1 testclass submitted... there is no set/get involved as far as I understand in here...I feel like I am overlooking something... any advice?
```
//class largest

import java.util.Scanner; // program uses class Scanner

public class Largest

{

public static void main ( String args[] )
{

int number; //sets the number as integer.
int sales; //sets sales as integer.
int largest;

int lagest = 0;
{

Scanner input = new Scanner (System.in ) ; //sets Scanner

System.out.print ("Please enter 1st sales figure: " );
int	number1 = input.nextInt ();

System.out.printf ("Please enter 2nd sales figure: " );
int	number2 = input.nextInt ();

System.out.printf ("Please enter 3rd sales figure: " );
int	number3 = input.nextInt ();

System.out.printf ("Please enter 4th sales figure: " );
int	number4 = input.nextInt ();

System.out.printf ("Please enter 5th sales figure: " );
int	number5 = input.nextInt ();

System.out.printf ("Please enter 6th sales figure: " );
int	number6 = input.nextInt ();

System.out.printf ("Please enter 7th sales figure: " );
int	number7 = input.nextInt ();

System.out.printf ("Please enter 8th sales figure: " );
int	number8 = input.nextInt ();

System.out.printf ("Please enter 9th sales figure: " );
int	number9 = input.nextInt ();

System.out.printf ("Please enter 10th sales figure: " );
int	number10 = input.nextInt ();

largest = 0;
{
if (number1 > largest);
largest = number1;
}
{

if (number2 > largest);

largest = number2 ;}

{

if (number3 > largest);

largest = number3 ;}

{

if (number4 > largest);

largest = number4 ;}

{

if (number5 > largest);

largest = number5 ;}

{

if (number6 > largest);

largest = number6 ;}

{
if (number7 > largest);

largest = number7 ;
}

{
if (number8 > largest);

largest = number8 ;
}

{
if (number9 > largest);

largest = number9 ;
}

{
if (number10 > largest);

largest = number10 ;
}

System.out.printf ("The Largest number is %d\n" , largest );
}

}
}

```

thanks again , I feel that if i submit this code, I do end up getting the largest number, but since I did not present them with counter and a test class, I will seem to have missed some of the points...

EDIT: I just noticed something is wrong, it's not taking the largest from one "if" to another... I need to work on that and will get back to you with the new code

This post has been edited by pumba: 08 June 2011 - 04:43 AM

### #4 pumba

Reputation: 0
• Posts: 28
• Joined: 08-June 11

## Re: Finding the largest number

Posted 08 June 2011 - 04:51 AM

Ok now my code is fixed, but I still am missing the counter and test class... any ideas?

```
//class largest

import java.util.Scanner; // program uses class Scanner

public class Largest

{

public static void main ( String args[] )
{

int number; //sets the number as integer.
int largest=0; //defining largest

{

Scanner input = new Scanner (System.in ) ; //sets Scanner

System.out.print ("Please enter 1st sales figure: " );
int	number1 = input.nextInt ();
if (number1 > largest)
largest = number1;

System.out.printf ("Please enter 2nd sales figure: " );
int	number2 = input.nextInt ();
if (number2 > largest)
largest = number2;

System.out.printf ("Please enter 3rd sales figure: " );
int	number3 = input.nextInt ();
if (number3 > largest)
largest = number3;

System.out.printf ("Please enter 4th sales figure: " );
int	number4 = input.nextInt ();
if (number4 > largest)
largest = number4;

System.out.printf ("Please enter 5th sales figure: " );
int	number5 = input.nextInt ();
if (number5 > largest)
largest = number5;

System.out.printf ("Please enter 6th sales figure: " );
int	number6 = input.nextInt ();
if (number6 > largest)
largest = number6;

System.out.printf ("Please enter 7th sales figure: " );
int	number7 = input.nextInt ();
if (number7 > largest)
largest = number7;

System.out.printf ("Please enter 8th sales figure: " );
int	number8 = input.nextInt ();
if (number8 > largest)
largest = number8;

System.out.printf ("Please enter 9th sales figure: " );
int	number9 = input.nextInt ();
if (number9 > largest)
largest = number9;

System.out.printf ("Please enter 10th sales figure: " );
int	number10 = input.nextInt ();
if (number10 > largest)
largest = number10;

System.out.printf ("The Largest number is %d\n" , largest );
}

}
}

```

### #5 CasiOo

• D.I.C Lover

Reputation: 1514
• Posts: 3,375
• Joined: 05-April 11

## Re: Finding the largest number

Posted 08 June 2011 - 05:11 AM

Try and make it with a loop (xor-logic showed you how to). Make a try and then ask again if you are still having problems.

also
17 {
72 }

these does nothing

### #6 itengineer

Reputation: 0
• Posts: 37
• Joined: 23-April 09

## Re: Finding the largest number

Posted 08 June 2011 - 05:12 AM

Some Tips which might be useful
1. Use an int array to store user inputs.
2. Use for loop to iterate over all the user inputs
Inside loop apply the if "largest" condition.

or in short for xor-logic algo.

Thanks
itengineer

### #7 jon.kiparsky

• Pancakes!

Reputation: 8939
• Posts: 15,450
• Joined: 19-March 11

## Re: Finding the largest number

Posted 08 June 2011 - 08:34 AM

Quote

1. Use an int array to store user inputs.

There's no use for an array in this application. He's just putting out the largest value, so only one int variable is needed.

Pumba, look at the repeated code:

``` System.out.print ("Please enter 1st sales figure: " );
int number1 = input.nextInt ();
if (number1 > largest)
largest = number1;
```

You're doing that ten times. You need to put it in a loop.

The loop you need is a for loop - I'm sure you've had this introduced to you before, but briefly it looks like this:

```for  ( initializer; continue condition; increment step)
{
// content to repeat
}
```

this can be unpacked into something like this:

```initialize a variable
while the continue condition is true,
// do the repeating content
// do the increment step
```

And in fact there is a construct exactly like this in Java, called a while loop, of which the for loop is a special case.

A typical example would be:
```for (int a = 0; a < 100; a++)
{
if (a%7 == 0) println a;
}
```

This simply starts a value at zero, and if that value is less than 100, it checks to see if it is evenly divisible by 7. If it is, it prints it. Then, it adds one to the variable and returns to check whether the value is less than 100, etc.
Now you don't need to use the value you in the loop - it can be just a counter.

Go ahead and try to use this to fix your program. Remember, too, that once you've checked an input and and handled it, you don't need that value any more. Either it's the current largest value or it's not - if so, it's assigned to largest, if not it can be forgotten. So you don't need input1, input2, etc. Just input will be enough.

### #8 xor-logic

• HAL9000 was an Apple product

Reputation: 128
• Posts: 764
• Joined: 04-February 10

## Re: Finding the largest number

Posted 08 June 2011 - 09:04 AM

pumba, I'm afraid you've misunderstood what I was saying you should do. Let me re-explain. As jon.kiparsky said, you're doing something 10 times (in a row). What are you doing 10 times? You're asking the user for a number and checking to see if it's the biggest number entered. Anything you're doing x times in a row should probably be put in a loop. Java has a few different kinds. Here's two that might help you with this program.

```while(coffee==notReady) {
System.out.println("Waiting...");
}
System.out.println("Finally! Delicious Coffee!");

```

In the above example, the code inside the brackets (print 'waiting...'), gets repeated as long as the condition in the parentheses (coffee==notReady) evaluates to true. Once it does, the program breaks out of the loop and moves on to the code after - in this case printing another inane statement.

You could use this with a counter, like so:
```int i = 0;
while (i < 10) {
System.out.println(i);
i++;
}

```

So while the counter variable (i) is less than 10, it repeats the code in the brackets - which is to printout the value of the counter, and then add 1 to it (i++ is shorthand for i = i + 1;).

BUT, Java has a loop designed to do stuff like that already. It's called the for loop, and jon.kiparsky showed it to you. I'll reiterate what he said.
```for (int i = 0; i < 42; i++) {
System.out.println(i);
}

```

Do you see that the counter variable is still there? The first part inside the parentheses says where to start. In this case, we're starting with i = 0. The middle part is EXACTLY like the while loop. Do this WHILE i < 42. The last part says to increment i by one everytime the loop runs. So this code will print out the numbers 0 - 41.

So, you probably want to use the for loop. If your instructor says there's no counter variable, smack him, because there is. The for loop depends on it.

So, let's take a look at what you need to do with your loop (what needs to be INSIDE the brackets):
-You need to get input from the user
-You need to figure out if that input is higher than any other input number.

Now, to figure out if your new number is higher than any other previous number, we don't have to compare it to each of them. The only one we have to care about is the previous highest number. If our new number is higher than the previously higher number, it's automatically higher than anything else. If it's not, it can't be the highest number. And if they're equal, well, it's still the highest, isn't it? All that to say, who cares about storing the number if it's not the highest? You don't need to. So you don't need variables for them. You only need the one variable, for storing the highest number.

So to bring back my pseudo-code from my first answer:
```//Very-pseudo code

//This is the variable where we're going to
//keep the largest number
Largest = 0

//here is where you would want to use one of those loops
do 10 times {

//Get the user input
number = user input

//Is the new number higher than the previously largest
//number?
number larger than Largest? {
//IT IS? Well store that shit in a variable, son!
//If it isn't, we don't care and we can do NOTHING with it.
Largest = number
}
}

//We get to this part only once the loop has done its thing.
Print "The largest number you put in was " + Largest

```

I hope you're getting a clearer idea of what you need to do. I'm trying to be as clear as I can without posting the answer (it's difficult), because I get the feeling you want to understand what you're doing as much as you want to actually do it, which is good.

If you still need more help, let us know.

Offhand, what itengineer said would work, but it's really unnecessarily complex in this situation. Put it aside for now, and maybe come back to it when you have some free time. You'll have to learn to do that if you need to create a program where you actually have to keep ALL the inputs for later.

This post has been edited by xor-logic: 08 June 2011 - 09:07 AM

### #9 pumba

Reputation: 0
• Posts: 28
• Joined: 08-June 11

## Re: Finding the largest number

Posted 08 June 2011 - 01:48 PM

Ok now I am feeling stupid...I just tried to apply the tips for the counter thing. When compiling I don't get any errors, when I run it, I get a blank screen... it just ignores me :

```//class largest

import java.util.Scanner; // program uses class Scanner

public class Largest

{

public static void main ( String args[] )
{

int number; //sets the number as integer.
int largest=0; //defining largest

Scanner input = new Scanner (System.in ) ; //sets Scanner
int counter = 0;
while (counter <= 10);

System.out.printf ("Please enter sales figure : ");
number = input.nextInt () ;

if  (number > largest) ;

largest = number;

counter++;

System.out.printf ("The largest number is %d\n" , largest );

}
}

```

### #10 jon.kiparsky

• Pancakes!

Reputation: 8939
• Posts: 15,450
• Joined: 19-March 11

## Re: Finding the largest number

Posted 08 June 2011 - 06:17 PM

```if  (number > largest) ;
```

That semicolon at the end of this means that if number > largest, you do a no-op - nothing happens. So nothing is ever assigned to largest. Otherwise, looks good. Although you might want to count through the loop and see how many times it executes.

Remember: it checks the condition at the top of the loop, and if it's true, it executes. Start from zero and walk through it until you exit. It's a minor mistake, but one worth catching and fixing.

### #11 pumba

Reputation: 0
• Posts: 28
• Joined: 08-June 11

## Re: Finding the largest number

Posted 12 June 2011 - 04:13 AM

Sorry for being away, had some sort of family emergency, anyway back now and having went through the comments, I tried a couple of things :

```//NO ERRORS AT COMPILING, BUT DOES NOTHING ON RUN

import java.util.Scanner; // program uses class Scanner

public class Largest

{

public static void main ( String args[] )
{

int number; //sets the number as integer.
int Largest=0; //defining largest

int counter ;

Scanner input = new Scanner (System.in ) ; //sets Scanner

counter = 0;
while (counter != 10);

System.out.println ("Please enter sales figure : ");
number = input.nextInt () ;

if  (Largest < number)

Largest = number;

counter++;

System.out.printf ("The largest number is %d\n" , Largest );

}
}

```

```//ENDLESS LOOP OF ENTER SALES FIGURE WITHOUT EVEN ALLOWING ME TO TYPE ANYTHING

import java.util.Scanner; // program uses class Scanner

public class Largest

{

public static void main ( String args[] )
{

int number; //sets the number as integer.
int Largest=0; //defining largest

int counter ;

Scanner input = new Scanner (System.in ) ; //sets Scanner

counter = 0;
while (counter != 10)

System.out.println ("Please enter sales figure : ");
number = input.nextInt () ;

if  (Largest < number)

Largest = number;

counter++;

System.out.printf ("The largest number is %d\n" , Largest );

}
}

```

frustrating to ave been away , i lost my line of thoughts... but hope you guys still have the will to help ... thanks

### #12 GregBrannon

• D.I.C Lover

Reputation: 2246
• Posts: 5,332
• Joined: 10-September 10

## Re: Finding the largest number

Posted 12 June 2011 - 04:22 AM

In your first new program (which looks like it'll work once you clean it up) the ';' at the end of your while statement makes it an 'empty loop' - it does nothing. The format should be:

```while( condition )
{
// do this while the condition is true
}
```

Edit: I verified that it does work if you clean up the while statement. Also, properly indenting your code and eliminating unnecessary white space will make your code much more readable and easier to understand.

Good luck!

This post has been edited by GregBrannon: 12 June 2011 - 04:29 AM

### #13 pumba

Reputation: 0
• Posts: 28
• Joined: 08-June 11

## Re: Finding the largest number

Posted 12 June 2011 - 06:35 AM

Ok great, now I have the counter set up and the code works well, counting exactly 10 and showing the largest number, now I have to divide my code in 2 parts, to have the class largest and the class largestTest ...

```//class largest

import java.util.Scanner; // program uses class Scanner

public class Largest

{

public static void main ( String args[] )
{

int number; //sets the number as integer.
int Largest=0; //defining largest
int counter ; //identifies counter

Scanner input = new Scanner (System.in ) ; //sets Scanner

counter = 1; //sets start counter
while (counter <= 10) //sets condition of loop / end counter

{

number = input.nextInt () ; // integer that user inputs becomes the number

if  (Largest < number) //condition for Largest

Largest = number; //if number larger than Largest, then Largest becomes number

counter++; //adds 1 to the counter, repeating above steps (asks for next data entry)

}
System.out.printf ("The biggest sales figure is: %d\n" , Largest ); //prints out the result (biggest sales figure = Largest) .
}
}

```

any tips? I tried to just putting the print out of largest in the test class, didn't know what largest is... if I put the whole code in the test, then the largest class is empty... a set/get is not needed here... will keep you posted when I figure out, meanwhile feel free to give me a push the right way

### #14 pumba

Reputation: 0
• Posts: 28
• Joined: 08-June 11

## Re: Finding the largest number

Posted 12 June 2011 - 07:24 AM

I guess am done, here's the outcome:

```//class largest

import java.util.Scanner; // program uses class Scanner

public class Largest //sets public class Largest

{

public void calculateLargest () //creates method calculateLargest
{

int number; //sets the number as integer.
int Largest=0; //defining largest
int counter ; //identifies counter

Scanner input = new Scanner (System.in ) ; //sets Scanner

counter = 1; //sets start counter
while (counter <= 10) //sets condition of loop / end counter

{

number = input.nextInt () ; // integer that user inputs becomes the number

if  (Largest < number) //condition for Largest

Largest = number; //if number larger than Largest, then Largest becomes number

counter++; //adds 1 to the counter, repeating above steps (asks for next data entry)
} // end while loop
System.out.printf ("The biggest sales figure is: %d\n" , Largest ); //prints out the result (biggest sales figure = Largest) .
} //end method calculateLargest
} //end public class Largest

```

```//class LargestTest

public class LargestTest
{

public static void main ( String args[] )
{

Largest bestseller = new Largest ();
bestseller.calculateLargest ();

}
}

```