Program to Reverse an Array using Recursion

Description

To reverse array using recursion, follow below program.

C/C++

/* C Program to Reverse an Array using Recursion */
//Save it as ReversalArrayUsingRecursion.c

#include<stdio.h>
int main(){

    int i, n;

    printf("Enter the size of array : ");
    scanf("%d",&n);

    int arr[n];

    printf("Enter the array elements : ");
    for(i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }

    printf("Entered elements of array : ");
    for(i=0;i<n;i++){
        printf("%d ",arr[i]);
    }

    reverseArrayUsingRecursion(arr, 0, (n-1));

    printf("\n");
    printf("The elements of array after reversal : ");
    for(i=0;i<n;i++) {
        printf("%d ",arr[i]);
    }
}

void reverseArrayUsingRecursion(int arr[], int start, int end){

    int temp;
    if(start >= end){
        return;
    }

    temp = arr[start];
    arr[start] = arr[end];
    arr[end] = temp;

    reverseArrayUsingRecursion(arr, start+1, end-1);
}
Input:
Enter the size of array : 
5
Enter the array elements : 
3
6
1
9
7

Output:
Entered elements of array : 
3 6 1 9 7 
The elements of array after reversal : 
7 9 1 6 3

Java

/* Java Program to Reverse an Array using Recursion */
//Save it as ReversalArrayUsingRecursion.java

import java.io.*;
import java.util.Scanner;

public class ReversalArrayUsingRecursion {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter the size of array : ");
        int n = scanner.nextInt();
        
        int arr[] = new int[n];
        
        System.out.println("Enter the array elements : ");
        for(int i=0;i<n;i++) {
            arr[i] = scanner.nextInt();
        }
        
        System.out.println("Entered elements of array : ");
        for(int i=0;i<n;i++) {
            System.out.print(arr[i]+" ");
        }
        
        reverseArrayUsingRecursion(arr, 0, (arr.length-1));
        
        System.out.println();
        System.out.println("The elements of array after reversal : ");
        for(int i=0;i<n;i++) {
            System.out.print(arr[i]+" ");
        }
    }

    private static void reverseArrayUsingRecursion(int[] arr, int start, int end) {
        
        int temp;
        
        if(start >= end) {
            return;
        }
        
        temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        
        reverseArrayUsingRecursion(arr, start+1, end-1);
    }
}
Input:
Enter the size of array : 
5
Enter the array elements : 
3
6
1
9
7

Output:
Entered elements of array : 
3 6 1 9 7 
The elements of array after reversal : 
7 9 1 6 3

Related Programs

1) Reversal of an Array
2) Program to reverse a number
3) Program to Read and Display entered numbers using an Array
4) Find the Average of Elements of Array
5) Program to form a number using entered digits
6) Program to merge two sorted arrays
7) Program to read and display 2D array
8) write a Program to find index of two array elements whose sum is equal to given value
9) Program to make pair of elements alternatively
10) Program to Print How Many Numbers Smaller than Current Number
11) Remove Duplicate Elements From Array
Share Me

Leave a Reply