C++ Program to Print an Array using Recursion Last Updated : 19 Mar, 2023 Comments Improve Suggest changes 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 C++ Program to Print an Array using Recursion P PranjalKumar4 Follow Improve Article Tags : Algorithms Recursion C++ Programs C++ DSA Arrays +2 More Practice Tags : CPPAlgorithmsArraysRecursion Similar Reads C++ Program to Find Factorial Using Recursion The factorial of a number is denoted by "n!" and it is the product of all positive integers less than or equal to n. In this article, we will learn how to find the factorial of a number using recursion in C++. Example Input: 5 Output: Factorial of 5 is 120 Factorial Using Recursion in C++The Factori 2 min read C++ Program To Print Reverse of a String Using Recursion Write a recursive function to print the reverse of a given string. Code: C++ // C++ program to reverse a string using recursion #include <bits/stdc++.h> using namespace std; /* Function to print reverse of the passed string */ void reverse(string str) { if(str.size() == 0) { return; } reverse 2 min read How to Reverse an Array using STL in C++? Reversing an array means rearranging its elements so that the first element becomes the last, the second element becomes the second last, and so on. In this article, we will learn how to reverse an array using STL in C++.The most efficient way to reverse an array using STL is by using reverse() func 2 min read C++ Program to Access Elements of an Array Using Pointer Prerequisites: Pointers in C++Array in C++ A Pointer is a variable that stores the memory location or address of an object or variable. In other words, pointers reference a memory location, and obtaining the value stored at that memory location is known as dereferencing the pointer. An Array is the 2 min read C++ Program For Sum of Natural Numbers Using Recursion Natural numbers include all positive integers from 1 to infinity. It does not include zero (0). Given a number n, find the sum of the first n natural numbers. To calculate the sum, we will use the recursive function recur_sum(). Examples: Input: 3Output: 6Explanation: 1 + 2 + 3 = 6 Input: 5Output: 1 1 min read All reverse permutations of an array using STL in C++ Given an array, the task is to print or display all the reverse permutations of this array using STL in C++. Reverse permutation means, for an array {1, 2, 3}: forward permutations: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 reverse permutations: 3 2 1 3 1 2 2 3 1 2 1 3 1 3 2 1 2 3 Examples: Input: a[] = { 3 min read C++ Program To Print Floyd's Triangle Floyd's triangle is a triangular array of natural numbers and it is named after Robert Floyd, a renowned computer scientist popularly known for the design of the FloydâWarshall algorithm. Here, we will see how to print Floyd's pattern triangle pyramid using the C++ program. Below are the examples: I 4 min read Sum of array Elements without using loops and recursion Given an array of N elements, the task is to find the Sum of N elements without using loops(for, while & doWhile) and recursion.Examples: Input: arr[]={1, 2, 3, 4, 5} Output: 15 Input: arr[]={10, 20, 30} Output: 60 j Approach: Unconditional Jump Statements can be used to solve this problem.Uncon 5 min read How to Print an Array in C++? In C++, an array is a fixed-size linear data structure that stores a collection of elements of the same type in contiguous memory locations. In this article, we will learn how to print an array in C++. For Example, Input: array = {10, 20, 30, 40, 50}Output: Array Elements: 10 20 30 40 50Printing Arr 2 min read How to Loop Over an Array in C++? In C++, an array is a data structure that stores elements of similar type in contiguous memory locations. We can access the elements of an array using array indexing. In this article, we will learn how to loop over an array in C++. Example: Input: int arr[] = [1, 2, 3, 4, 5] Output: Array Elements: 2 min read Like