Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
C++ Program to Find Factorial of a Large Number Using Recursion
Next article icon

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 

C++ Program 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 1

2. 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)



Next Article
C++ Program to Find Factorial of a Large Number Using Recursion

P

PranjalKumar4
Improve
Article Tags :
  • Algorithms
  • Arrays
  • C++
  • C++ Programs
  • DSA
  • Recursion
Practice Tags :
  • CPP
  • Algorithms
  • Arrays
  • Recursion

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++ Code // 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; }
    2 min read
  • C++ Program to Find Factorial of a Large Number Using Recursion
    Given a large number N, task is to find the factorial of N using recursion. Factorial of a non-negative integer is the multiplication of all integers smaller than or equal to n. For example factorial of 6 is 6*5*4*3*2*1 which is 720. Examples: Input : N = 100Output : 93326215443944152681699238856266
    2 min read
  • Programs for Printing Pyramid Patterns using Recursion
    This article is aimed at giving a recursive implementation for pattern printing. Simple triangle pattern: C/C++ Code // C++ code to demonstrate star pattern #include <iostream> using namespace std; // function to print a row void printn(int num) { // base case if (num == 0) return; cout <
    15+ 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() fun
    3 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences