Example HTML page

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.

#include<stdio.h>
int main()
{
    int arr[10] = {10,5,25,35,45,48,65,100,66,22};
    int i, min;

    min = arr[0];

    for(i = 0; i < 10; i++)
    {
        // if value of current element is less than previous value
        // then assign new value to min
        if(arr[i] < min)
        {
            min = arr[i];
        }

        }

      printf("Smallest value = %d", min);

    // signal to operating system everything works fine
    return 0;
}
Finding the smallest element in a given array.

Output:

Command Prompt
Smallest value = 5
——————————–
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.

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest