C Program to Find Largest Element in an Array Last Updated : 20 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will learn how to find the largest element in the array using a C program.The simplest method to find the largest element in the array is by iterating the array and comparing each element with the assumed maximum and updating it when the element is greater. C #include <stdio.h> int findMax(int arr[], int n) { // Assume the first element is the largest int max = arr[0]; for (int i = 1; i < n; i++) { // Update max if arr[i] is greater if (arr[i] > max) { max = arr[i]; } } return max; } int main() { int arr[] = {5, 2, 7, 6}; int n = sizeof(arr) / sizeof(arr[0]); printf("%d\n", findMax(arr, n)); return 0; } Output7 Explanation: This method is simply the linear search algorithm modified to find the minimum element.There are also a few more methods to find the largest element in the array that are useful in different cases. Let’s take a look at them one by one:Table of ContentUsing SortingUsing RecursionUsing RecursionThe recursive approach works similar to the above method. It compares the element one by one using recursion and updates the assumed maximum if the larger value is found. C #include <stdio.h> // Recursive approach to find the maximum element int findMax(int arr[], int n) { // Base case: Only one element if (n == 1) return arr[0]; // Find maximum from the rest of the array int max = findMax(arr, n - 1); // Return smaller element between curent element // or maximum element in rest of the array return arr[n - 1] > max ? arr[n - 1] : max; } int main() { int arr[] = {5, 2, 7, 6}; int n = sizeof(arr) / sizeof(arr[0]); // Finding and printing the maximum element printf("%d\n", findMax(arr, n)); return 0; } Output7 Using Sortingin an array sorted in ascending order, the maximum element is present at the end i.e. (n - 1)th index. The array can be sorted using qsort() function with a custom comparator for ascending order. C #include <stdio.h> #include <stdlib.h> // Comparator function for qsort (ascending order) int compare(const void *a, const void *b) { return (*(int *)a - *(int *)b); } int findmax(int arr[], int n) { // Sort the array using qsort qsort(arr, n, sizeof(int), compare); // The last element is largest after sorting return arr[n - 1]; } int main() { int arr[] = {5, 2, 7, 6}; int n = sizeof(arr) / sizeof(arr[0]); // Find and print the maximum element in arr printf("%d\n", findmax(arr, n)); return 0; } Output7 This method is less efficient than linear search for just finding the maximum element but more suitable for finding the n-th largest element. Comment More infoAdvertise with us Next Article C Program to Find Largest Element in an Array K kartik Follow Improve Article Tags : C Programs C Language C Array Programs Similar Reads Program to find largest element in an array using Dynamic Memory Allocation Given an array arr[] consisting of N integers, the task is to find the largest element in the given array using Dynamic Memory Allocation. Examples: Input: arr[] = {4, 5, 6, 7} Output: 7Explanation:The largest element present in the given array is 7. Input: arr[] = {8, 9, 10, 12} Output: 12Explanati 5 min read C Program to Find the Maximum and Minimum Element in the Array In this article, we will discuss different ways to find the maximum and minimum elements of the array in C.The simplest method to find the maximum and minimum element of the array is iterates through the array and compare each element with the assumed minimum and maximum and update them if the curre 3 min read C Program To Find Next Greater Element Given an array, print the Next Greater Element (NGE) for every element. The Next greater Element for an element x is the first greater element on the right side of x in the array. Elements for which no greater element exist, consider the next greater element as -1. Examples: For an array, the righ 5 min read C Program to Sort an Array in Ascending Order Sorting an array in ascending order means arranging the elements in the order from smallest element to largest element.The easiest way to sort an array in C is by using qsort() function. This function needs a comparator to know how to compare the values of the array. Let's look at a simple example:C 3 min read C Program to Sort the Elements of an Array in Descending Order Sort an array in descending order means arranging the elements in such a way that the largest element at first place, second largest at second place and so on. In this article, we will learn different ways to sort an array in descending order in C.The simplest method to sort the array in descending 3 min read Like