C++ Program to print an Array using Recursion
Write a program in C++ to print an Array using Recursion
- Using static variable: Static variables have a 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++ Program to print// an Array using Recursion#include <bits/stdc++.h>usingnamespacestd;// Recursive function to print the arrayvoidprint_array(intarr[],intsize){// using the static variablestaticinti;// base caseif(i == size) {i = 0;cout << endl;return;}// print the ith elementcout << arr[i] <<" ";i++;// recursive callprint_array(arr, size);}// Driver codeintmain(){intarr[] = { 3, 5, 6, 8, 1 };intn =sizeof(arr) /sizeof(arr[0]);print_array(arr, n);return0;}chevron_rightfilter_noneOutput:3 5 6 8 1
- Without using Static variable:
// C++ Program to print// an Array using Recursion#include <bits/stdc++.h>usingnamespacestd;// Recursive function to print the arrayvoidprint_array(intarr[],intsize,inti){// base caseif(i == size) {cout << endl;return;}// print the ith elementcout << arr[i] <<" ";i++;// recursive callprint_array(arr, size, i);}// Driver codeintmain(){intarr[] = { 3, 5, 6, 8, 1 };intn =sizeof(arr) /sizeof(arr[0]);print_array(arr, n, 0);return0;}chevron_rightfilter_noneOutput:3 5 6 8 1
Recommended Posts:
- Program to check if an array is palindrome or not using Recursion
- Program to print Sum of even and odd elements in an array
- Program to print Sum Triangle for a given array
- Program to print an array in Pendulum Arrangement
- Print 1 to 100 in C++, without loop and recursion
- Program to print product of even and odd indexed elements in an Array
- How to print N times without using loops or recursion ?
- Program to print an array in Pendulum Arrangement with constant space
- Print alternate nodes of a linked list using recursion
- Programs to print Triangle and Diamond patterns using recursion
- Print a character n times without using loop, recursion or goto in C++
- Print a number 100 times without using loop, recursion and macro expansion in C?
- Program to Calculate e^x by Recursion
- Program for length of a string using recursion
- Mean of array using recursion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


