In: Computer Science
Example:
Input |
Output |
7 1 5 3 6 9 22 2 |
Min: 1 Max: 22 |
C++ preferred
C++ code:
#include <iostream>
using namespace std;
int main()
{
//initializing n,min,max and number
int n,min,max,num;
//accepting n
cin>>n;
//accepting first number
cin>>num;
//assigning it to max
max=num;
//assigning it to min
min=num;
//looping from 0 to num-1
for(int i=0;i<n-1;i++){
//accepting number
cin>>num;
//checking if it is greater than max
if(num>max)
//assigning num to max
max=num;
//checking if it is less than min
if(num<min)
//assigning num to min
min=num;
}
//printing Min
cout<<"Min: "<<min<<endl;
//printing Max
cout<<"Max: "<<max<<endl;
return 0;
}
Screenshot:
Input and Output: