i want to finding maximum and minimum values of 10 numbers using for loop and the program work wrong !! any help?!
#include <iostream>
using namespace std;
int main() {
int x;
int max = -999999999;
int min= 999999999;
for(int i=0;i<10;i++){
cin>>x;
if (x > max)
max = x;
else if(x<min)
min = x;
}
cout<<"minimum is "<<min<<endl;
cout << "Maximum is " << max << endl;
#include <iostream>
#include <limits>
usingnamespace std;
int main() {
int x;
int max = std::numeric_limits<int>::min() ;
int min = std::numeric_limits<int>::max() ;
for( int i=0; i<10; i++)
{
cin >> x;
if (x > max)
max = x;
if( x < min )
min = x;
}
cout<<"minimum is "<< min << endl;
cout << "Maximum is " << max << endl;
}
Note that there is no else in the loop. It is possible for a value to be both greater than max and less than min. For instance, if you enter one value: 1. On the only iteration of the loop, both max and min should be set to that value.