Program to Find the Largest Element of an Array using Recursion

Program to Find the Largest Element of an Array using Recursion


Consider the following code in which we have swapped two numbers without using recursion:

CODE:

# include <stdio. h>

# include <conio. h>

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

void main ()

{

   int arr [5];

   int i, max=0;

   clrscr ();

   printf (“Enter 5 numbers”);

   for (i = 0; i < 5; i ++)

   {

      scanf (“%d”, &arr [i]);

   }

   max = Largest (arr, 5);

   printf (“The largest element is %d”, max);

   getch ();

}

int Largest (int *arr, int size)

{

   static int i = 0, max = -999;

   if (i < size)

   {

      if ( max < *(arr + i) )

      {

         max = *(arr + i);

      }

      i ++;

      Largest (arr, size);

   }

  return max;

}

OUTPUT:

Enter 5 numbers 23 45 67 23 7 23

The largest element is 67