Write C++ program to print elements of array using recursion

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
using namespace std;
 
#define MAX_SIZE 100
 
//Function declaration
void PrintArray(int arr[], int start, int len);
 
 
int main()
{
    int arr[MAX_SIZE];
    int num, i;
 
    // Inputting size and elements in array
    cout<<"Enter size of the array: ";
    cin>>num;
 
    cout<<"Enter elements in the array: ";
    for(i=0; i<num; i++)
    {
        cin>>arr[i];
    }
 
    // Printing array recursively
    cout<<"Elements in the array: ";
    PrintArray(arr, 0, num);
 
    return 0;
}
 
// Printing array recursively within a given range.
 
void PrintArray(int arr[], int start, int len)
{
    // Recursion base condition
    if(start >= len)
        return;
 
    // Printing the current array element
    cout<<arr[start]<<"\t";
 
    // Recursively calling printArray to print next element in the array
    PrintArray(arr, start + 1, len);
}

Result

Write C++ program to print elements of array using recursion
Write C++ program to print elements of array using recursion

Leave a Comment