An **array** is a data structure containing a collection of values or variables. The simplest type of array is a linear array or one-dimensional array. An array can be defined in C with the following syntax:

**int Arr[5] = {12, 56, 34, 78, 100};**

/* here 12,56,34,78,100 are the elements at indices 0,1,2,3,4 respectively */

In this example, array **Arr** is a collection of 5 integers. Each integer can be identified and accessed by its index. The indices of the array start with 0, so the first element of the array will have index 0, the next will have index 1 and so on.

**Largest element of the array** is the array element which has the largest numerical value among all the array elements.

**Examples:**

If we are entering **5** elements (N = 5), with array element values as **12, 56, 34, 78 and 100**

Then, **largest element present in the given array is: 100**

If we are entering **4** elements (N = 4), with array element values as **1, 6, 3, and 2**

Then, **largest element present in the given array is: 6**

Write a C program to find the largest element in the given array.

In this program, we have to find the largest element present in the array. We will do this by first saving the value of the first element in the variable **‘largest’**. 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 N-1 times and the program ends.

The sequence of steps for the solution will be as follows:

1. Create an array of user-defined size.

2. Run the for loop till the user-defined size to insert the element at each location.

3. Considering the first element of the array to be the largest, compare all the remaining elements of the array, and change the largest value if assumed largest element is smaller than the element being compared.

4. At last, the largest element will hold the actual largest value in the array. Thus, print it.

There are several ways to find the largest element of an array in C language. Let’s take a detailed look at all the approaches to find the largest element of an array.

- Largest Element of an Array in C using Loops
- Largest Element of an Array in C using Recursion
- Largest Element of an Array in C using Function
- Largest Element of an Array in C using Pointers

In this approach, at first we assume that the first element is the largest. After that we traverse the array and if the current element is greater than the largest element, we update the largest element.

**Example:**

Given **Array: [1,2,4,1,6,4]**

Initially, Largest Element: 1

Now we start traversing the array.

**Index: 1 **

Index element > Largest Element, so updating the largest element with 2.

**Index: 2**

Index element > Largest Element, so updating the largest element with 4.

**Index: 3**

Index element < Largest Element, so Largest element remains the same.

**Index: 4**

Index element > Largest Element, so updating the largest element with 6.

**Index: 5**

Index element < Largest Element, so Largest element remains the same.

So **Largest Element** is 6.

Here is the source code of the C Program to find the largest number in an array using loops. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

`/*`

`* C Program to find the largest number in an array using loops`

`*/`

`#include <stdio.h>`

int main()

`{`

int size, i, largest;

printf("\n Enter the size of the array: ");

scanf("%d", &size);

int array[size]; //Declaring array

`//Input array elements`

printf("\n Enter %d elements of the array: \n", size);

for (i = 0; i < size; i++)

`{`

scanf(" %d", &array[i]);

`}`

`//Declaring Largest element as the first element`

largest = array[0];

for (i = 1; i < size; i++)

`{`

if (largest < array[i])

largest = array[i];

`}`

printf("\n largest element present in the given array is : %d", largest);

return 0;

`}`

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

2. Then, initialize an array of size given 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 **arraySize-1**, extracting array element one by one and comparing it to the **largest** 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 hold the actual largest value present in the array.

**Time Complexity: O(n)**

In the program, we are traversing the array so the time complexity is O(n).

**Space Complexity: O(n)**

Space is required to store the array, so space complexity is O(n).

Here is the runtime output of the C program where the user is reading array of **6** elements with values as **1, 2, 4, 1, 6, and 4**. Then it finds out the largest element and displays its value.

Enter the size of the array: 6 Enter 5 elements of the array: 1 2 4 1 6 4 largest element present in the given array is: 6

In the recursive approach, the recursive function returns the max of the current element and the rest of the elements with the base case to stop when the array is traversed.

**Example:**

**Array:** [7,2,4,1,5]

return max( 7 , max( [2,4,1,5] )) –––––––––––––––––––––––––––– 7 return max( 2 , max( [4,1,5]) –––––––––––––––––––– 5 return max( 4, max( [1,5]) –––––––––– 5 return max(1,max( [5]) ––– 5 return 5

Here is the source code of the C Program to find the largest number in an array using recursion. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

`/*`

`* C Program to find the largest number in an array using recursion`

`*/`

`#include <stdio.h>`

`#include <math.h>`

`//Function to find max of two numbers`

int max(int a,int b)

`{`

if(a>b)

return a;

`else`

return b;

`}`

int largest(int arr[],int size,int i)

`{`

if(i == size-1)

return arr[i];

return max(arr[i], largest(arr,size,i+1));

`}`

int main()

`{`

int size;

printf("Enter Size of Array: ");

scanf("%d",&size);

`//Declaring array`

int arr[size];

`//Input array elements`

printf("Enter Array Elements: ");

for(int i=0;i<size;i++)

scanf("%d",&arr[i]);

printf("Largest Element is: %d",largest(arr,size,0));

return 0;

`}`

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

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

3. Declare a function to find the largest element of the array which returns **max** between the **current element** and all other elements with base stops when the array is traversed.

4. Print the value returned by the function as the largest element.

**Time Complexity: O(n)**

As there are n function calls, the time complexity is O(n).

**Space Complexity: O(n)**

Space is required to store the array, so space complexity is O(n).

Here is the runtime output of the C program where the user is reading array of **5** elements with values as **7, 2, 4, 1, and 5**. Then it finds out the largest element and displays its value.

Enter the size of the array: 5 Enter Array Elements: 7 2 4 1 5 Largest Element is: 7

In this approach, we declare a function in which we assume that the first element of an array is the largest. After that we traverse the array and if the current element is greater than the largest element, we update the largest element. At the end of the function, return the largest element.

**Example:**

**Array: [11, 34, 21, 100]**

Initially, Largest Element: 11

Now we start traversing the array.

**Index: 1**

Index element > Largest Element, so updating the largest element with 34.

**Index: 2**

Index element < Largest Element, so largest element remains the same i.e. 34.

**Index: 3**

Index element > Largest Element, so updating the largest element with 100

So **Largest Element** is 100

Here is the source code of the C Program to find the largest number in an array using function. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

`/*`

`* C Program to find the largest number in an array using function`

`*/`

`#include <stdio.h>`

`#include <math.h>`

int largest(int arr[],int size)

`{`

`//Declaring Largest element as the first element`

int largest=arr[0];

for(int i=1;i<size;i++)

`{`

`//Updating largest element if current element is greater than largest element`

if(arr[i] > largest)

largest=arr[i];

`}`

return largest;

`}`

int main()

`{`

int size;

printf("Enter Size of Array: ");

scanf("%d",&size);

`//Declaring array`

int arr[size];

`//Input array elements`

printf("Enter Array Elements: ");

for(int i=0;i<size;i++)

scanf("%d",&arr[i]);

printf("Largest Element is: %d",largest(arr,size));

return 0;

`}`

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

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

3. Declare the function to find the largest element.

4. Inside the function, initialize the largest element as the first element of the array.

5. Traverse the array and if the current element is greater than largest element, update the largest element.

6. Return and print the largest element.

**Time Complexity: O(n)**

In the program, we are traversing the array, so the time complexity is O(n).

**Space Complexity: O(n)**

Space is required to store the array, so space complexity is O(n).

Here is the runtime output of the C program where the user is reading array of **4** elements with values as **11, 34, 21, and 100**. Then it finds out the largest element and displays its value.

Enter the size of the array: 4 Enter Array Elements: 11 34 21 100 Largest Element is: 100

In this approach, we find the largest element of an array using the pointers. We declare a function to find the largest element which takes the array, its size and pointer to the largest element. We traverse the complete array and if current element is greater than the largest element, we update the largest element pointer. At first we assume that the first element is the largest. After that we traverse the array and if the current element is greater than the largest element, we update the largest element.

**Example:**

**Array: [2, 5, 21, 9]**

Initially, Largest Element: 2

Now we start traversing the array.

**Index: 1**

Index element > Largest Element, so updating the largest element with 5.

**Index: 2**

Index element > Largest Element, so updating the largest element with 21.

**Index: 3**

Index element < Largest Element, so Largest element remains the same i.e 21.

So **Largest Element** is 21

Here is the source code of the C Program to find the largest number in an array using pointers. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

`/*`

`* C Program to find the largest number in an array using pointers`

`*/`

`#include <stdio.h>`

`#include <math.h>`

void largest_elem(int arr[],int size,int *largest)

`{`

`//Declaring Largest element as the first element`

*largest=arr[0];

for(int i=1;i<size;i++)

`{`

`//Updating largest element if current element is greater than largest element`

if(arr[i] > *largest)

*largest=arr[i];

`}`

`}`

int main()

`{`

int size;

printf("Enter Size of Array: ");

scanf("%d",&size);

`//Declaring array`

int arr[size],largest;

`//Input array elements`

printf("Enter Array Elements: ");

for(int i=0;i<size;i++)

scanf("%d",&arr[i]);

largest_elem(arr,size,&largest);

printf("Largest Element is: %d",largest);

return 0;

`}`

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

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

3. Declare a function to find the largest element which takes an array, its size and pointer to the largest element as the parameter.

4. In the function:

- Initialize largest element as the first element of the array.
- Traverse the array and if the current element is greater than the largest element,update the largest element.

5. Print the Largest Element.

**Time Complexity: O(n)**

In the program, we are traversing the array, so the time complexity is O(n).

**Space Complexity: O(n)**

Space is required to store the array, so space complexity is O(n).

Here is the runtime output of the C program where the user is reading array of **4** elements with values as **2, 5, 21, and 9**. Then it finds out the largest element and displays its value.

Enter the size of the array: 4 Enter Array Elements: 2 5 21 9 Largest Element is: 21

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

**If you find any mistake above, kindly email to [email protected]**

**Related Posts:**

- Apply for Computer Science Internship
- Check C Books
- Check Computer Science Books
- Apply for C Internship
- Practice Computer Science MCQs