C++ Program to find second largest number in an array


In this program, You will learn how to find second largest number in an array in C++.


List is: 10 20 30 40 50 60 70

Second largest number is :60


Example: How to find second largest number in an array in C++

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

int main() {

    int array[5], i, j;
    int slar, temp;

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

    for (i = 0; i < 5; i++) {
        for (j = i + 1; j < 5; j++) {
            if (array[i] > array[j]) {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
    cout << "\nSecond Largest Number is :" << array[5 - 2];

    return 0;
}

Output:

Enter any 5 Numbers :2 3 1 2 8
Second Largest Number is :3