Monday, 29 May 2017

Largest Number of an Array ~ GNIITHELP

Program to Find the Largest Element of an Array using Recursion

You can do this in many ways, Let's learn how to do this using recursion.
#include<stdio.h>
#include<conio.h>

int findLargest(int arr[],int size);

void main()
{
   int arr[5];
   int i, max=0;
   clrscr();
   printf("Enter 5 numbers\t");
   for(i=0; i<5; i++)
   {
      scanf("%d", &arr[i]);
   }
   max = findLargest(arr, 5);
   printf("The largest element is %d", max);
   getch();
}

int findLargest(int *arr,int size)
{
   static int i=0, max=-999;
   if(i < size)
   {
      if( max < *(arr+i) )
      {
         max = *(arr+i);
      }
      i++;
      findLargest(arr, size);
   }
  return max;
}
Output
Enter 5 numbers 209 194 185 167 198
The largest element is 209

No comments:

Post a Comment