C++ Program to find the largest and smallest element in the array
In this program, You will learn how to find largest and smallest element in array in C++.
List is: 10 20 30 40 50 60 70
Largest & Smallest is:70 10
Example: How to find the largest and smallest element in the array in C++.
#include<iostream>
using namespace std;
int main() {
int array[5], i;
int lg, sm;
cout << "Enter 5 numbers:";
for (i = 0; i < 5; i++) {
cin >> array[i];
}
lg = array[0];
sm = array[0];
for (i = 0; i < 5; i++) {
if (lg < array[i]) {
lg = array[i];
}
if (sm > array[i]) {
sm = array[i];
}
}
cout << "Largest element is:" << lg;
cout << "\nSmallest element is:" << sm;
return 0;
}
Output:
Enter 5 numbers:33 10 40 50 20
Largest element is:50
Smallest element is:10