C++ Program to find largest and smallest element in array


In this program, You will learn how to find largest and smallest element in array in C++.


List is: 10 20 30 40 50 60 70

Largest & Smallest is :70 10


Example: How to find largest and smallest element in array in C++

Run it Now
#include<iostream>
using namespace std;

int main() {

    int array[5], i;
    int lg, sm;

    cout << "Enter any 5 Numbers :";
    for (i = 0; i < 5; i++) {
        cin >> array[i];
    }

    lg = array[0];
    sm = array[0];

    for (i = 0; i < 5; i++) {
        if (lg < array[i]) {
            lg = array[i];
        }

        if (sm > array[i]) {
            sm = array[i];
        }
    }
    cout << "\nLargest Element is :" << lg;
    cout << "\nSmallest Element is :" << sm;

    return 0;
}

Output:

Enter any 5 Numbers :1 2 3 4 5
Largest Element is :5
Smallest Element is :1