# C Program to Find the Biggest Number in an Array of Numbers using Recursion

«
»

This is a C Program which prints the largest number in an unsorted array of elements using recursion.

Problem Description

This program will implement a one-dimentional array defining elements in unsorted fashion, in which we need to find largest element using Recursion method. The array used here is of type integer.

Problem Solution

1. Create an array, taking its size from the users and define all its elements.
2. Now make a function passing three parameters, array, last index of array and largest element of the array.
3. Assuming first element to be the largest element in the array, call this function.
4. Inside this function, since first element has been selected as largest for now, check if the element at index passed to this function (initially last index) is greater than the largest number passed. Update the largest number and proceed to call the same function, but now with index-1.
5. This way each element is checked from index (size-1) to 1 is compared with largest number. The base condition for this recursive function would be to return the largest number when it reaches the first index.

Program/Source Code

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

1. ` `
2. `    /*`
3. `     * C Program to find the Biggest Number in an Array of Numbers using `
4. `     * Recursion`
5. `     */`
6. ` `
7. `    #include <stdio.h>`
8. `    int large(int[], int, int);`
9. ` `
10. `    int main()`
11. `    {`
12. ` `
13. `        int size;`
14. `        int largest;`
15. `        int list;`
16. `        int i;`
17. ` `
18. `        printf("Enter size of the list:");`
19. `        scanf("%d", &size);`
20. ` `
21. `        printf("Printing the list:\n");`
22. `        for (i = 0; i < size ; i++) `
23. `        {`
24. `            list[i] = rand() % size;`
25. `            printf("%d \t", list[i]);`
26. `        }`
27. ` `
28. `        if (size == 0) `
29. `        {`
30. `            printf("Empty list\n");`
31. `        }`
32. ` `
33. `        else `
34. `        {`
35. `            largest = list;`
36. `            largest = large(list, size - 1, largest);`
37. `            printf("\nThe largest number in the list is: %d\n", largest);`
38. `        }`
39. ` `
40. `    }`
41. ` `
42. `    int large(int list[], int position, int largest)`
43. `    {`
44. ` `
45. `        if (position == 0)`
46. `            return largest;`
47. ` `
48. `        if (position > 0) `
49. `        {`
50. `            if (list[position] > largest)`
51. `            {`
52. `                largest = list[position];`
53. `            }`
54. `            return large(list, position - 1, largest);`
55. `        }`
56. ` `
57. `    }`
Program Explanation

1. Declare an array of some fixed capacity, 20 and take size from the users and define elements in unsorted fashion using rand() functions to insert random number less than size ( rand() % size )
2. Assume first element of the array to be the largest number.
2. Create a function with three parameters i.e the array, last index of array (size -1) and largest element of the array.
4. Call this function.
5. Inside it, the largest number is compared with the element at position passed to the function (initially the position passed is the last index of the array) and make necessary updation if largest number is smaller than the number compared.
6. After comparison, this function is again called inside itself but with position being previous position(last index)-1.
7. This way all elements are compared with largest number making updation wherever needed via recursion.
8. This process stops when the variable position reaches the 0th position.

Runtime Test Cases
```Enter size of the list:8
Printing the list:
7	6	1	3	1	7	2	4
The largest number in the list is: 7```

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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