C++ Program to Print an Array using Recursion Last Updated : 12 Jul, 2025 Comments Improve Suggest changes 12 Likes Like Report Write a program in C++ to print an Array using Recursion 1. Using Static Variable Static variables have the property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.Syntax: static data_type var_name = var_value; C++ // C++ Program to print // an Array using Recursion #include <bits/stdc++.h> using namespace std; // Recursive function to print the array void print_array(int arr[], int size) { // using the static variable static int i; // base case if (i == size) { i = 0; cout << endl; return; } // print the ith element cout << arr[i] << " "; i++; // recursive call print_array(arr, size); } // Driver code int main() { int arr[] = { 3, 5, 6, 8, 1 }; int n = sizeof(arr) / sizeof(arr[0]); print_array(arr, n); return 0; } Output:3 5 6 8 12. Without using Static Variable C++ // C++ Program to print // an Array using Recursion #include <bits/stdc++.h> using namespace std; // Recursive function to print the array void print_array(int arr[], int size, int i) { // base case if (i == size) { cout << endl; return; } // print the ith element cout << arr[i] << " "; i++; // recursive call print_array(arr, size, i); } // Driver code int main() { int arr[] = { 3, 5, 6, 8, 1 }; int n = sizeof(arr) / sizeof(arr[0]); print_array(arr, n, 0); return 0; } Output:3 5 6 8 1 Time Complexity: O(n)Auxiliary Space: O(1), If we consider recursive call stack then it would be O(n) Create Quiz Comment P PranjalKumar4 Follow 12 Improve P PranjalKumar4 Follow 12 Improve Article Tags : C++ Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like