# C Program to Find the Largest Number in an Array

«
»

This is a C Program to find the largest number in an array.

Problem Description

We have to write a program in C such that the program will read a one-dimensional array and find out the largest element present in the array.

Expected Input and Output

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

Problem Solution

Fundamentally, 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 = {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.

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.

Program/Source Code

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

1. `/*`
2. ` * C program to read N integers into an array A and`
3. ` * a) Find the sum of all numbers`
4. ` * b) Find the average of all numbers`
5. ` * Display the results with suitable headings`
6. ` */`
7. ` `
8. `#include <stdio.h>`
9. ` `
10. `int main()`
11. `{`
12. ` `
13. `        int size, i, largest;`
14. ` `
15. `        printf("\n Enter the size of the array: ");`
16. `        scanf("%d", &size);`
17. `        int array[size];`
18. ` `
19. `        printf("\n Enter %d elements of  the array: \n", size);`
20. ` `
21. `        for (i = 0; i < size; i++)`
22. `        {   `
23. `            scanf("%d", &array[i]);`
24. `        }`
25. ` `
26. `        largest = array;`
27. ` `
28. `        for (i = 1; i < size; i++) `
29. `        {`
30. `            if (largest < array[i])`
31. `            largest = array[i];`
32. `        }`
33. ` `
34. `        printf("\n largest element present in the given array is : %d", largest);`
35. ` `
36. `        return 0;`
37. ` `
38. `}`
Program Explanation

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.

Runtime Test Cases

Here is the runtime output of the C program where the user is reading array of 5 elements with values as 12, 56, 34, 78 and 100. Then it finds out the largest element and displays its value.

```Enter the size of the array: 5

Enter 5 elements of  the array:
12
56
34
78
100

largest element present in the given array is: 100```

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms 