C Program To Print The Largest Element In An Array

In the provided array, initially, the variable "max" will store the value 25. During the 1st iteration, the value 11 will be compared with "max"; as 11 is smaller than "max", "max" will remain unchanged. Moving on to the subsequent iteration, the value 7 will be compared with "max" as well; since 7 is also smaller than "max", there will be no alteration to the value of "max." Following this, "max" will be compared against 75. Since 75 is greater than the current value of "max," "max" will be updated to hold the value 75. This comparison process will continue until the array has been entirely traversed. Upon completion of the loop, "max" will contain the largest element present 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: max = arr[0]
  • STEP 5: SET i=0. REPEAT STEP 6 and STEP 7 i<length
  • STEP 6: if(arr[i]>max) max=arr[i]
  • STEP 7: i=i+1.
  • STEP 8: PRINT "Largest element in given array:" assigning max.
  • STEP 9: RETURN 0
  • STEP 9: 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 max with first element of array.  
    
        int max = arr[0];  
    
          
    
        //Loop through the array  
    
        for (int i = 0; i < length; i++) {   
    
            //Compare elements of array with max  
    
           if(arr[i] > max)  
    
               max = arr[i];  
    
        }    
    
        printf("Largest element present in given array: %d\n", max);  
    
        return 0;  
    
    }
    

Output:

Output

Largest element present in given array: 75

Input Required

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