C Program To Print The Smallest Element In An Array

Consider the given array. Initially, the variable 'min' is assigned the value 25. During the first iteration, 'min' is compared with 11. As 11 is smaller than 25, 'min' is updated to 11. Moving to the second iteration, 11 is compared with 7. Since 7 is smaller than 11, 'min' is now set to 7. This process continues until all elements in the array have been checked. Finally, 'min' will store the smallest value found in the array.

ALGORITHM:

  • STEP 1: START
  • STEP 2: INITIALIZE arr = {25, 11, 7, 75, 56}
  • STEP 3: length= sizeof(arr)/sizeof(arr[0])
  • STEP 4: min = arr[0]
  • STEP 5: SET i=0. REPEAT STEP 6 and STEP 7 UNTIL i<length
  • STEP 6: if(arr[i]<min) min=arr[i]
  • STEP 7: i=i+1.
  • STEP 8: PRINT "Smallest element present in given array:" by assigning min
  • STEP 9: RETURN 0.
  • STEP 10: END.
  • PROGRAM:

    Example
    
    #include <stdio.h>  
    
       
    
    int main()  
    
    {  
    
        //Initialize array   
    
        int arr[] = {25, 11, 7, 75, 56};    
    
          
    
        //Calculate length of array arr  
    
        int length = sizeof(arr)/sizeof(arr[0]);  
    
          
    
        //Initialize min with first element of array.  
    
        int min = arr[0];  
    
          
    
        //Loop through the array  
    
        for (int i = 0; i < length; i++) {   
    
            //Compare elements of array with min  
    
           if(arr[i] < min)  
    
               min = arr[i];  
    
        }    
    
        printf("Smallest element present in given array: %d\n", min);  
    
        return 0;  
    
    }
    

Output:

Output

Smallest element present in given array: 7

Input Required

This code uses input(). Please provide values below: