C++ program to read n numbers from user and find maximum and minimum number among them using concept of array

#include <iostream>
using namespace std;
int main()
{
    int n,i;
    cout<<"Enter n: ";
    cin>>n;
    int num[n];
    for(i=0;i<n;i++)
    {
        cout<<"Enter "<<i<<" number:";
        cin>>num[i];
    }
    
    int max=num[0];
    for(i=1;i<n;i++)
    {
        if(max<num[i])
            max=num[i];
    }
    cout<<"The maximum number is "<<max<<endl;

    int min=num[0];
    for(i=1;i<n;i++)
    {
        if(min>num[i])
            min=num[i];
    }
    cout<<"The minimum number is "<<min;
    return 0;
}

No comments:

Post a Comment