C++ Program to find the largest and smallest element in the 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 the largest and smallest element in the array in C++.

#include<iostream>
using namespace std;

int main() {

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

    cout << "Enter 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 << "Largest element is:" << lg;
    cout << "\nSmallest element is:" << sm;

    return 0;
}

Output:

Enter 5 numbers:33 10 40 50 20
Largest element is:50
Smallest element is:10

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.