C++ Program to find 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 second smallest 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 Smallest Number is :" << array[1];

    return 0;
}

Output:

Enter any 5 Numbers :2 3 4 1 5
Second Smallest Number is :2