12,352,294 members (53,188 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 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 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 18-Dec-12 7:16am

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

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

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 564 Sergey Alexandrovich Kryukov 301 Maciej Los 250 Dave Kreskowiak 150 Karthik Bangalore 149
 OriginalGriff 9,219 Sergey Alexandrovich Kryukov 6,299 Dave Kreskowiak 3,044 ppolymorphe 2,256 Karthik Bangalore 2,251

Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 18 Dec 2012