C Program to find largest element in an array

Largest element in an array

Today, we will learn how to find the largest element present in an array. We will do this by first taking the value of the first element in the variable largest_ele. Then we will compare with remaining elements of the array and store the value if another larger number is found in this array. This will go on array_size-1 times and the program ends.

C Program to find Largest element in an array

Working:-

1. Take the size of the array from the user as a input.

2. Then initialize an array of size provided by the user.

3. Using for loop take array element as input from users and insert them into the array.

4. After inserting all the elements of the array consider the very first element of array to be the largest.

5. Run a for loop from 1 to array_size-1, extracting array element one by one and comparing it to the element.

6. If the largest element is smaller than the element being compared, then the largest element is updated with the value of the current element of the array.

7. In the end, the largest element will take the actual largest value present in the array.

C Program:-

#include <stdio.h>
int main()
    {
        int size, i, largest_ele;
        printf("\n Enter the size of the array: ");     //taking size of array
        scanf("%d", &size);
        
        int array[size];
        printf("\n Enter %d elements of  the array: \n", size);     //print size and take input from user
        for (i = 0; i < size; i++)
        {   
            scanf("%d", &array[i]);
        }
        largest_ele = array[0];
        for (i = 1; i < size; i++)      //loop for checking largest element 
        {
            if (largest_ele < array[i])
            largest_ele = array[i];
        }
        printf("\n largest element in array is : %d", largest_ele);     //print largest element in array 
        return 0;
    }
Output:
Enter the size of the array : 5
Enter 5 elements of  the array:
52
24
12
36
95
largest element in array is : 95