Find Smallest and Largest Number in Array
Here you will get and learn the example code of C++ program to find smallest and largest number in array. First we will create 5 numbers array list by user input, find smallest and largest number using for loop, print the result.
C++ program to Find Smallest and Largest Number in Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include<iostream> using namespace std; int main() { int j, k=0, small=0,large=0, arr[5], item; cout<<"\nInput 5 Numbers : "; for(j=0; j<5;j++) { cin>>arr[j]; } small=arr[0]; for(j=0; j<5;j++) { if(arr[j]<small) { small=arr[j]; } if(arr[j]>large) { large=arr[j]; } } cout<<"\nSmallest Number is : "<<small; cout<<"\nLargest Number is : "<<large; return 0; } |
Output
Input 5 Numbers : 2 3 4 1 6
Smallest Number is : 1
Largest Number is : 6
Check out our other C++ programming Examples