12,632,090 members (26,249 online)
Rate this:
Please Sign up or sign in to vote.
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 22:45pm
Updated 17-Dec-12 23:37pm
v2

## 2 solutions

Rate this:
Please Sign up or sign in to vote.

## 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.
Comments
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:
Please Sign up or sign in to vote.

## 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
Comments
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 255 Richard MacCutchan 118 Peter Leow 85 Admire Mhlaba 75 F. Xaver 65
 OriginalGriff 1,999 ppolymorphe 1,242 Peter Leow 804 John Simmons / outlaw programmer 665 CPallini 571

Advertise | Privacy | Mobile
Web01 | 2.8.161208.2 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100