C++ Program to find the 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 the second largest number in an array in C++.

#include<iostream>
using namespace std;

int main() {

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

    cout << "Enter 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 << "Second largest:" << array[5 - 2];

    return 0;
}

Output:

Enter numbers:10 20 50 30 15
Second largest:30