# Thread: finding maximum,minimum and average of 10 numbers

1. Member
Join Date
Nov 2012
Posts
4
Rep Power
0

## finding maximum,minimum and average of 10 numbers

hello,i am a complete beginner to java programming.what's wrong with this program is how do i find the minimum of the given numbers.what value do i need to assign to the min variable?any help would be appriciated.

Java Code:
```import java.util.Scanner;
public class onem {
public static void main (String[]args) {
Scanner k=new Scanner(System.in);

int sum=0;
int i=1;
int min=0;
int max=0;
int avg=0;

while(i<=10) {
int n;
System.out.println("write a number");
n=k.nextInt();

if (n<min) {
n=min;

}
else{}
if (n>max) {
max=n;
}
sum=sum+n;
i++;
}
avg=sum/10;
System.out.println(min);
System.out.println(avg);
System.out.println(max);

}
}```

2. ## Re: finding maximum,minimum and average of 10 numbers

There are a ton of ways to handle this case. I bet if you think about it a little harder you'll get it. What have you tried?

3. Moderator
Join Date
Apr 2009
Posts
13,069
Rep Power
23

## Re: finding maximum,minimum and average of 10 numbers

Java Code:
```    if (n<min) {
n=min;
}```
This is the opposite way round to how you've done the 'max' part in the following lines.

4. Member
Join Date
Nov 2012
Posts
4
Rep Power
0

## Re: finding maximum,minimum and average of 10 numbers

i have tried

Java Code:
```if (n<min) //here's the problem{
n=min;
}```
but the problem is variable min is set to 0.so the output is always 0.but the goal of the program is to find the minimum of the given numbers.so how do i set the variable min to the variable n.

5. Moderator
Join Date
Apr 2009
Posts
13,069
Rep Power
23

## Re: finding maximum,minimum and average of 10 numbers

No.
You want to change the 'min' value if 'n' is smaller.
At the moment you are changing the 'n' value.

6. ## Re: finding maximum,minimum and average of 10 numbers

Assignments work 'from right to left', i.e. a= b means that a gets the value of b; so in your example it should be;

Java Code:
```if (n < min)
min= n;```
kind regards,

Jos

7. ## Re: finding maximum,minimum and average of 10 numbers

Originally Posted by xxbakugan
Java Code:
`else{}`
Blech!

8. Member
Join Date
Nov 2012
Posts
4
Rep Power
0

## Re: finding maximum,minimum and average of 10 numbers

Java Code:
`int min=0;`
here's the thing.the minimum is always 0.how to set min to n?

Today, 04:16 AMJunky
Re: finding maximum,minimum and average of 10 numbers

Originally Posted by xxbakugan
Java Code:
1
else{}
Blech!

9. Moderator
Join Date
Apr 2009
Posts
13,069
Rep Power
23

## Re: finding maximum,minimum and average of 10 numbers

Also (and I have to admit to only thinking of this last night)...
Java Code:
`int min = 0;`
Unless you can guarantee that there is going to be at least one 'n' less than 0 then the 'min' value will always be 0 as n will never be less than 0.
You should assign a really big number to 'min', and a really big negative number to 'max' (the Integer class has a couple of constants).

10. Member
Join Date
Nov 2012
Posts
4
Rep Power
0

## Re: finding maximum,minimum and average of 10 numbers

Originally Posted by Tolls
Also (and I have to admit to only thinking of this last night)...
Java Code:
`int min = 0;`
Unless you can guarantee that there is going to be at least one 'n' less than 0 then the 'min' value will always be 0 as n will never be less than 0.
You should assign a really big number to 'min', and a really big negative number to 'max' (the Integer class has a couple of constants).
thanks a lot.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•