# C++ Program to Find the Smallest Number in an Array

Howdy readers, in this post you will learn how to write a C++ program to find the smallest number in an array.

The below program asks the user to enter the size of the array, then it asks to enter the value of each of the element. After that, this program finds the smallest element of the array using a for loop statement.

So, without any delay, let’s begin this tutorial.

## C++ Program to Find the Smallest Number in an Array

C++ Program

```// C++ Program to Find the Smallest Number in an Array
#include <iostream>
using namespace std;

int main(){
int arr, i, size, min;

// Taking input from the user
cout << "Enter the size of the array: ";
cin >> size;

cout << "Enter the elements of the array: \n";
for (i = 0; i < size; i++){
cin >> arr[i];
}

min = arr;
for (i = 0; i < size; i++){
if (arr[i] < min){
min = arr[i];
}
}

// Displaying output
cout << "Smallest element in the array is: " << min << endl;

return 0;
}
```

Output

```Enter the size of the array: 6
Enter the elements of the array:
9
64
23
35
41
39
Smallest element in the array is: 9
```

Explanation

```    int arr, i, size, min;
```

In this program, we have declared one array and three int data type variables named `arr`, `i`, `size` and `min` respectively.

```    // Taking input from the user
cout << "Enter the size of the array: ";
cin >> size;
```

Then, the user is asked to enter the size of the array. This value gets stored in the `size` named variable.

```    cout << "Enter the elements of the array: \n";
for (i = 0; i < size; i++){
cin >> arr[i];
}
```

Now, this program prompts the user to enter every element of the array.

```    min = arr;
for (i = 0; i < size; i++){
if (arr[i] < min){
min = arr[i];
}
}
```

Now, here comes the part where we will find the smallest number of the array. We assign the value of the first element as the smallest.

Then, we use a for loop to compare the value of the first element with the remaining elements.

If the value of any of the remaining elements is smaller than the first element, then that value will be updated to `min`.

This process keeps on executing until it checks all the elements of the array.

Suppose, we have an array A = [3, 2, 1]. Then, according to the above logic, we have: min = arr = 3.

• Now, 1st For Loop iteration: for (i = 1; i <3; i++), here the condition is True. So, the loop continues.
• If (arr < min)  = (2 < 3), here the condition is also True.
• So, min = arr = 2.
• 2nd For Loop iteration: for (i = 2; i < 3; i++), here the condition is True.
• If (arr < min) = (1 < 2), here the condition is also True.
• So, min = arr = 1
• 3rd For Loop iteration: for(i = 3; i < 3; i++), here the condition is False. So, the loop terminates and we get min = 1 which is the smallest element of the given array.
```    // Displaying output
cout << "Smallest element in the array is: " << min << endl;
```

After finding out the smallest number in the array, we print it down on the screen using the cout statement.

## Conclusion

I hope after reading this post, you understand how to write a C++ program to find the smallest number in an array.

If you have any queries regarding the tutorial, then let us know in the comment section. We will be pleased to solve all of your queries.