In this post, we will learn how to find the smallest of N numbers using C++ Programming language.
This program takes an array as input and prints the smallest element of the array using for loop statement.
So, without further ado, let’s begin this tutorial.
C++ Program to Find Smallest of N Numbers
// C++ Program to Find Smallest of N Numbers #include <iostream> using namespace std; int main(){ int arr[10], count, i, min; // Asking for input cout << "Enter the size of the array: "; cin >> count; cout << "Enter the elements of the array: \n"; for (i = 0; i < count; i++){ cin >> arr[i]; } min = arr[0]; for (i = 0; i < count; i++){ if (arr[i] < min){ min = arr[i]; } } cout << "Smallest Element: " << min; return 0; }
Output
Enter the size of the array: 5
Enter the elements of the array:
12
17
5
21
9
Smallest Element: 5
How Does This Program Work ?
int arr[10], count, i, min;
In this program, we have declared some variables named count, i and min.
// Asking for input cout << "Enter the size of the array: "; cin >> count; cout << "Enter the elements of the array: \n"; for (i = 0; i < count; i++){ cin >> arr[i]; }
Then, the user is asked to enter the size of the array and the elements of the array.
for (i = 0; i < count; i++){ if (arr[i] < min){ min = arr[i]; } }
We calculate the smallest numbers of the array using for loop statement.
cout << "Smallest Element: " << min;
Finally, the smallest element of the array which is stored in min named variable is displayed on the screen using cout statement.
Conclusion
I hope after going through this post, you understand how to find the smallest element in an array using C++ Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.
Also Read: