Write a C Program To Reverse an Array Using Recursion
In this tutorial, we are going to see how to write a C program to reverse an array using recursion. For example, if ‘arr’ is an array of integers with three elements such as:
arr[0] = 1 arr[1] = 2 arr[2] = 3
Then, by reversing the array we will have:
arr[0] = 3 arr[1] = 2 arr[2] = 1
There are four ways to reverse an array in C, by using for loop, pointers, recursion, or by creating a function.
Write a C Program To Reverse an Array Using Recursion
#include <stdio.h>
// recursive function to reverse an Array
void reverse(int i, int n, int arr[])
{
if(i==n)
{
return ;
}
else
{
reverse(i+1, n, arr);
printf("%d ", arr[i]);
}
}
int main()
{
int i, n, arr[50];
printf("Enter size of array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("After reversing the array: ");
// call the recursive function
reverse(0, n, arr);
return 0;
}
Output:
Enter the number of elements in the array: 3 Enter the elements of the array: 1 2 3 The reversed array is: 3 2 1




