C++ Program to find largest and smallest number in an array using function


In this program, You will learn how to find the largest and smallest number in an array using the function in C++.


List is :2 3 4 5 10 33 44 55 66 77

Largest & Smallest is: 77 2


Example: How to find largest and smallest number in an array using function in C++

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

void findNumber(int array[], int size) {

    int lg = array[0];
    int sm = array[0];

    for (int i = 0; i < size; i++) {
        if (lg < array[i]) {
            lg = array[i];
        }

        if (sm > array[i]) {
            sm = array[i];
        }
    }
    cout << "\nLargest Number is :" << lg;
    cout << "\nSmallest Number is :" << sm;
}

int main() {

    int array[10], i, size;

    cout << "Enter Size of an array :";
    cin>>size;

    cout << "\nEnter Array elements :";
    for (i = 0; i < size; i++) {
        cin >> array[i];
    }

    findNumber(array, size);

    return 0;
}

Output:

Enter Size of an array :5
Enter Array elements :2 3 44 1 6
Largest Number is :44
Smallest Number is :1