C Program to find largest number in array using Recursion

By | November 29, 2016

Largest number in array using Recursion


Write a C Program to find largest number in array using Recursion. Here’s simple Program to find largest number in  array using Recursion in C Programming Language.


Recursion : :


  • Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
  • The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.
  • Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.

Problem : :


This C Program prints the largest number in an unsorted array of elements using recursion. The array used here is of type integer.

Here is the source code of the C Program to find largest number in array using Recursion. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.


SOURCE CODE : :


/*  C Program to find largest number in array using Recursion  */

#include <stdio.h>

int large(int[], int, int);

int main()
{
    int size;
    int largest;
    int list[20];
    int i;

    printf("Enter size of the list :: ");
    scanf("%d", &size);
    printf("\nPrinting the list :: \n\n");
    for (i = 0; i < size ; i++)
    {
        list[i] = rand() % size;
        printf("\t%d", list[i]);
    }
    if (size == 0)
    {
        printf("\nEmpty list\n");
    }
    else
    {
        largest = list[0];
        largest = large(list, size - 1, largest);
        printf("\n\nThe largest number in the list is :: %d \n", largest);
    }
    
    return 0;
}
int large(int list[], int size, int largest)
{
    if (size == 1)
        return largest;

    if (size > -1)
    {
        if (list[size] > largest)
        {
            largest = list[size];
        }
        return(largest = large(list, size - 1, largest));
    }
    else
    {
        return largest;
    }
}

Output : :


/*  C Program to find largest number in an array using Recursion  */

Enter size of the list :: 10

Printing the list ::

        1       7       4       0       9       4       8       8       2       4

The largest number in the list is :: 9

Process returned 0

Above is the source code for C Program to find largest number in array using Recursion which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

Leave a Reply