C program to find maximum and minimum number using recursion

C program to find maximum and minimum number using recursion


Input:

Enter the number of elements:

5
8 7 2 43 5

Output:


Maximum number is 43

Minimum number is 2

Implementation:


#include <stdio.h>
#include <limits.h>
#define MX 1000000

int max=INT_MIN;
int min=INT_MAX;

int findmax(int a[],int n)
{
 if(n<0)
  return max;
 if(a[n]>max)
 {
  max=a[n];
 }
 return findmax(a,n-1);
}

int findmin(int a[],int n)
{
 if(n<0)
  return min;
 if(a[n]<min)
 {
  min=a[n];
 }
 return findmin(a,n-1);
}

int main(void) {
 int a[MX],n,i;
 printf("Enter the number of elements:\n");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
  scanf("%d",&a[i]);
 }
 printf("Maximum number is %d\n",findmax(a,n-1));
 printf("Minimum number is %d\n",findmin(a,n-1));
 return 0;
}