Write a program to find the largest element in an array using Recursion in C.
Write a program to find the largest element in an array using Recursion in C.
Course Code: MCS-011
Course Title: Problem Solving and Programming
1 Answers
Best Answer
#include<stdio.h>
#define MAX 100
int getMaxElement(int []);
int size;
int main(){
int arr[MAX],max,i;
printf("Enter the size of the array: ");
scanf("%d",&size);
printf("Enter %d elements of an array: ", size);
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
max=getMaxElement(arr);
printf("Largest element of an array is: %d",max);
return 0;
}
int getMaxElement(int arr[]){
static int i=0,max =-9999;
if(i < size){
if(max<arr[i])
max=arr[i];
i++;
getMaxElement(arr);
}
return max;
}
Sample output:
Enter the size of the array: 6
Enter 5 elements of an array: 2 8 10 9 6 1
Largest element of an array is: 10