Sunday, 17 February 2019

C++ Program to Find the largest and smallest element in an array using 1D array

#include <iostream>

using namespace std;

int main()
{
    int a[100],mx,mn,i,n;

    cout<<"Enter the number of elements\n";
    cin>>n;

    cout<<"\nEnter the array elements \n";
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }

    mx=mn=a[0];

    for(i=0;i<n;i++)
    {
        if(mx < a[i]) mx=a[i];
        if(mn > a[i]) mn=a[i];
    }

    cout<<"\nLargest Number is "<<mx;
    cout<<"\nSmallest Number is "<<mn;

    return 0;
}

OUTPUT:

Enter the number of elements
5

Enter the array elements
6
1
3
5
9

Largest Number is 9
Smallest Number is 1