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 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 .
Recursion : :
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 : :
Output : :
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 .
need an explanation for this answer? contact us directly to get an explanation for this answer