C++ Program to print an Array using Recursion - GeeksforGeeks

11 Oct, 2019
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>using namespace std;  // Recursive function to print the arrayvoid 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 codeint 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 1
Without using Static variable:// C++ Program to print// an Array using Recursion  #include <bits/stdc++.h>using namespace std;  // Recursive function to print the arrayvoid 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 codeint 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

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>using namespace std;  // Recursive function to print the arrayvoid 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 codeint 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 1

Syntax:
static data_type var_name = var_value; 
// C++ Program to print// an Array using Recursion  #include <bits/stdc++.h>using namespace std;  // Recursive function to print the arrayvoid 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 codeint main(){      int arr[] = { 3, 5, 6, 8, 1 };    int n = sizeof(arr) / sizeof(arr[0]);      print_array(arr, n);      return 0;}
3 5 6 8 1

Without using Static variable:// C++ Program to print// an Array using Recursion  #include <bits/stdc++.h>using namespace std;  // Recursive function to print the arrayvoid 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 codeint 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

// C++ Program to print// an Array using Recursion  #include <bits/stdc++.h>using namespace std;  // Recursive function to print the arrayvoid 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 codeint main(){      int arr[] = { 3, 5, 6, 8, 1 };    int n = sizeof(arr) / sizeof(arr[0]);      print_array(arr, n, 0);      return 0;}
3 5 6 8 1

Arrays
C++
Recursion
Arrays
Recursion
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Next Greater Element
Window Sliding Technique
Count pairs with given sum
Program to find sum of elements in a given array
Reversal algorithm for array rotation
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
Map in C++ Standard Template Library (STL)
Inheritance in C++
std::sort() in C++ STL