C Program to find the minimum (or smallest) element in an array.
In this article, we are going to write a C Program to find the smallest element in a given array.
Coming to mathematics i am giving you a list of elements as follows.
100, 60,55,30,20,10,5
Among this, find the smallest element. How you can do this. Initially assume that the first element is the minimum element.
Then we go for the second element that is ’60’ here.
we compare this with first element. 100>60, so we assume that the minimum value is now ’60’
Then we go to the next element in the array i.e. 55.
60>55 now the minimum value is 55.
what we are doing here is we have taken the first value as minimum, and we are comparing this element with the other elements in the array and finding the minimum element.
The same thing we are doing in the following program.
Output:
——————————–
Process exited after 0.1691 seconds with return value 0
Press any key to continue . . .
We have taken an array with name ‘arr’ and initialized with ’10’ elements. we assigned the min value with ‘arr[0]’ i.e. the first element in the array. Now we are comparing each element in the array with this value; if the array element is less than the ‘min’ value,then ‘min’ is assigned with the array element.
Finally we are printing the smallest value, which is in ‘min’ variable.
can you do it for finding the maximum element in a given array. Just replace the condition you are almost finished the program.
Related Program:
Program to find the maximum element in a given array.