# how to find the min and the max and its position without using math+ while using while • ### Question

• I wrote

using System;
class MainClass
{
public static void Main (string[] args)
{
int max=0,min=0,minp=0,maxp=0,num;
Console.Write("give me a positive number: ");
while(num!=0)
{
if(num!=0)
{
if(max<num)
{
max=num;
maxp++;
}
if(min>num)
{
min=num;
minp++;
}
Console.Write("give me a positive number: ");
}
}
Console.WriteLine("the Max is: {0} and his position is:{1}",max,maxp);
Console.WriteLine("the Min is: {0} and his position is: {1}",min,minp);
}

}

and the result was when I entered:

2,4,6,9,0

The Max is : 9 and his position is : 4

The Min is : 0 and his position is : 0

how to make it right?..

Thursday, January 17, 2019 12:56 PM

• The suggestion by tommytwotrain is a correct analysis of your problem, and
using min=999999 should be adequate for your current application as it appears
to be limited by design to relatively small numbers. However, for future
reference note that *in general* when wanting to ensure that the min is taken
from the set of numbers input at run time it is usually best to set the initial
value of min to the highest possible positive number for the data type.

For example, if you set min=999999 and you enter values that are equal to or
greater than 999999 then your program will give incorrect results. e.g. -

give me a positive number: 999999
give me a positive number: 9999991
give me a positive number: 9999992
give me a positive number: 9999993
give me a positive number: 0
the Max is: 9999993 and his position is:4
the Min is: 999999 and his position is: 0

give me a positive number: 9999991
give me a positive number: 9999992
give me a positive number: 9999993
give me a positive number: 9999994
give me a positive number: 0
the Max is: 9999994 and his position is:4
the Min is: 999999 and his position is: 0

So to ensure correct results regardless of the values entered, use:

min = int.MaxValue

If min is a different data type use the MaxValue member for that type.

Also for future reference, when posting code please use the "Insert Code Block"
button on the forum editor menu bar. That will ensure that the code is kept
formatted correctly.

- Wayne
Thursday, January 17, 2019 11:37 PM
• Your min position is never set since min = 0 initially. Try

`int max=0,min=999999,minp=0,maxp=0,num;`

• Proposed as answer by Thursday, January 17, 2019 2:38 PM
• Marked as answer by Saturday, January 19, 2019 11:04 PM
Thursday, January 17, 2019 2:20 PM

### All replies

• Your min position is never set since min = 0 initially. Try

`int max=0,min=999999,minp=0,maxp=0,num;`

• Proposed as answer by Thursday, January 17, 2019 2:38 PM
• Marked as answer by Saturday, January 19, 2019 11:04 PM
Thursday, January 17, 2019 2:20 PM
• The suggestion by tommytwotrain is a correct analysis of your problem, and
using min=999999 should be adequate for your current application as it appears
to be limited by design to relatively small numbers. However, for future
reference note that *in general* when wanting to ensure that the min is taken
from the set of numbers input at run time it is usually best to set the initial
value of min to the highest possible positive number for the data type.

For example, if you set min=999999 and you enter values that are equal to or
greater than 999999 then your program will give incorrect results. e.g. -

give me a positive number: 999999
give me a positive number: 9999991
give me a positive number: 9999992
give me a positive number: 9999993
give me a positive number: 0
the Max is: 9999993 and his position is:4
the Min is: 999999 and his position is: 0

give me a positive number: 9999991
give me a positive number: 9999992
give me a positive number: 9999993
give me a positive number: 9999994
give me a positive number: 0
the Max is: 9999994 and his position is:4
the Min is: 999999 and his position is: 0

So to ensure correct results regardless of the values entered, use:

min = int.MaxValue

If min is a different data type use the MaxValue member for that type.

Also for future reference, when posting code please use the "Insert Code Block"
button on the forum editor menu bar. That will ensure that the code is kept
formatted correctly.

- Wayne
Thursday, January 17, 2019 11:37 PM
• thank you very much!

mmay172

Saturday, January 19, 2019 11:27 AM
• thank you!!

mmay172

Saturday, January 19, 2019 11:27 AM
•   