11,637,662 members (70,158 online)
Rate this:
See more:
how can i compare more than two variables in loops? to find out the largest an minimum number here is my code which takes 10 inputs from the user .. Now how can i find out maximum(largest) and minimum (smallest) between them?????

```#include<iostream>
using namespace std;

int main()

{
int max=0,min=0;
int num;
int counter =0;
cout<<"Enter 10 numbers"<<endl;
while(counter<10)
{

cin>>num;
counter++;
}
return 0;
}```
Posted 17-Dec-12 21:45pm
Edited 17-Dec-12 22:37pm
v2

Rate this:

## Solution 1

You can compare as many as you want. You need to reword this, with code, so it makes sense. Typically, you create a variable for the biggest and one for the smallest, and every one, you replace the current value if the new one is a better match.
Malik Yousaf at 18-Dec-12 4:19am

here i wrote a code which takes 10 inputs from the user now how can i find maximum and minimum number between them???
Christian Graus at 18-Dec-12 14:21pm

I told you how and someone else ( foolishly in my view ) wrote the code for you. If you could not write that code, you need to talk to your teacher, you're struggling.
Rate this:

## Solution 4

```#include<iostream>
#include<climits>
using namespace std;

int main()
{
int max=INT_MIN,min=INT_MAX;
int num;
int counter =0;
cout<<"Enter 10 numbers"<<endl;
while(counter<10)
{

cin>>num;
if ( max < num) max = num;
if ( min > num) min = num;
counter++;
}
cout << "min " << min << ", max " << max << endl;
return 0;
}```
v2
Malik Yousaf at 18-Dec-12 7:16am

Thank you very much.
can you tell me other way without using climits??
CPallini at 18-Dec-12 8:15am

Of course there are many ways. But why? Why don't you want `climits` header?
Philippe Mori at 18-Dec-12 20:22pm

If counter is 0, you can initialize both min and max with num.