C Program to Find Largest Element in an Array 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.

  2.     /*
  3.      * C Program to find the Biggest Number in an Array of Numbers using 
  4.      * Recursion
  5.      */
  7.     #include <stdio.h>
  8.     int large(int[], int, int);
  10.     int main()
  11.     {
  13.         int size;
  14.         int largest;
  15.         int list[20];
  16.         int i;
  18.         printf("Enter size of the list:");
  19.         scanf("%d", &size);
  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.         }
  28.         if (size == 0) 
  29.         {
  30.             printf("Empty list\n");
  31.         }
  33.         else 
  34.         {
  35.             largest = list[0];
  36.             largest = large(list, size - 1, largest);
  37.             printf("\nThe largest number in the list is: %d\n", largest);
  38.         }
  40.     }
  42.     int large(int list[], int position, int largest)
  43.     {
  45.         if (position == 0)
  46.             return largest;
  48.         if (position > 0) 
  49.         {
  50.             if (list[position] > largest)
  51.             {
  52.                 largest = list[position];
  53.             }
  54.             return large(list, position - 1, largest);
  55.         }
  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 Books in C Programming, Data Structures and Algorithms.

Note: Join free Sanfoundry classes at Telegram or Youtube

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.