Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?????

C#
#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
Updated 17-Dec-12 22:37pm
v2

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.
 
Share this answer
 
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.
C#
#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;
 }
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900