C++ Program to Print an Array using Recursion Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article 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) Comment More infoAdvertise with us Next Article Output of C++ programs | Set 43 (Decision and Control Statements) P PranjalKumar4 Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads C++ Return 2D Array From Function An array is the collection of similar data-type stored in continuous memory. And when we are storing an array inside an array it is called 2 D array or 2-dimensional array. To know more about arrays refer to the article Array in C++. When there is a need to return a 2D array from a function it is al 4 min read Output of C++ programs | Set 42 Prerequisite : Pointers and References Q.1 What Is The Output Of this program? CPP #include <iostream> using namespace std; void fun(int& a, int b) { a += 2; b += 1; } int main() { int x = 10, y = 2; fun(x, y); cout << x << " " << y << " "; fun(x 4 min read Output of C++ programs | Set 43 (Decision and Control Statements) Decision and Loops & Control Statements QUE.1 What is the output of this program ? CPP #include <iostream> using namespace std; int main () { int n; for (n = 5; n > 0; n--) { cout << n; if (n == 3) break; } return 0; } OPTION a) 543 b) 54 c) 5432 d) 53 Answer: a Explanation : In t 3 min read array::at() in C++ STL Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::at() This function is used to return the reference to the element present at the position given as the par 2 min read Output of C programs | Set 60 (Constants) Prerequisite: C Constants and StringsQ.1 What is the output of this program? C#include <stdio.h> int main() { const char *s = ""; char str[] = "Hello"; s = str; while(*s) printf("%c", *s++); return 0; } Options a) Error b) H c) Hello d) Hel ans:- c Explanation const char *s = "";The constant v 3 min read C/C++ program for calling main() in main() Given a number N, the task is to write C/C++ program to print the number from N to 1 by calling the main() function using recursion.Examples: Input: N = 10 Output: 10 9 8 7 6 5 4 3 2 1Input: N = 5 Output: 5 4 3 2 1 Approach:Use static variable to initialise the given number N.Print the number N and 2 min read Like