# finding maximum,minimum and average of 10 numbers

• 11-01-2012, 06:02 PM
xxbakugan
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.

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);       } }```
• 11-01-2012, 06:52 PM
KevinWorkman
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?
• 11-01-2012, 06:56 PM
Tolls
Re: finding maximum,minimum and average of 10 numbers
Code:

```    if (n<min) {     n=min;     }```
This is the opposite way round to how you've done the 'max' part in the following lines.
• 11-01-2012, 07:13 PM
xxbakugan
Re: finding maximum,minimum and average of 10 numbers
i have tried

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.
• 11-01-2012, 07:29 PM
Tolls
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.
• 11-01-2012, 07:36 PM
JosAH
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;

Code:

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

Jos
• 11-02-2012, 12:16 AM
Junky
Re: finding maximum,minimum and average of 10 numbers
Quote:

Originally Posted by xxbakugan
Code:

`else{}`

Blech!
• 11-02-2012, 10:44 AM
xxbakugan
Re: finding maximum,minimum and average of 10 numbers
Code:

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

Quote:

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

Originally Posted by xxbakugan
Quote:

Java Code:
1
else{}
Blech!
:(-:
• 11-02-2012, 11:03 AM
Tolls
Re: finding maximum,minimum and average of 10 numbers
Also (and I have to admit to only thinking of this last night)...
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).
• 11-02-2012, 11:13 AM
xxbakugan
Re: finding maximum,minimum and average of 10 numbers
Quote:

Originally Posted by Tolls
Also (and I have to admit to only thinking of this last night)...
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.