C++ Program to find the second smallest number in an array


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


List is: 10 20 30 40 50 60 70 80

Second smallest number is:20

Example: How to find the second smallest number in an array in C++.

#include<iostream>
using namespace std;

int main() {

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

    cout << "Enter 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 smallest number is:" << array[1];

    return 0;
}

Output:

Enter 5 numbers:10 40 50 20 30
Second smallest number is:20