how to find the min and the max position without using math+ while using while loop

  • Thread starter Thread starter mmay172
  • Start date Start date
M

mmay172

Guest

I wrote this

when the input was:

2,12,6,80,21,0(the program stops when the user inserts 0)

the result was

the Max is: 80 and his position is:5( the position supposed to be 4 )

the Min is: 2 and his position is: 1

in another case when the input was:

3,13,7,2,80,12,0

the result was

the Max is: 80 and his position is:5

the Min is: 2 and his position is: 2(the position supposed to be 4)




using System;

class MainClass
{
public static void Main (string[] args)
{
int max=0,min=999999,minp=0,maxp=0,num,i=1;
Console.Write("give me a positive number: ");
num=int.Parse(Console.ReadLine());
while(num!=0)
{
i++;
if(max<num)
{
max=num;
maxp++;
}
if(min>num)
{
min=num;
minp++;
}
Console.Write("give me a positive number: ");
num=int.Parse(Console.ReadLine());
}
maxp=i-minp;
minp=i-maxp;
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);
}
}


mmay172

Continue reading...
 
Back
Top