Write C++ program to print elements of array using recursion


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #define MAX_SIZE 100
  5.  
  6. //Function declaration
  7. void PrintArray(int arr[], int start, int len);
  8.  
  9.  
  10. int main()
  11. {
  12. int arr[MAX_SIZE];
  13. int num, i;
  14.  
  15. // Inputting size and elements in array
  16. cout<<"Enter size of the array: ";
  17. cin>>num;
  18.  
  19. cout<<"Enter elements in the array: ";
  20. for(i=0; i<num; i++)
  21. {
  22. cin>>arr[i];
  23. }
  24.  
  25. // Printing array recursively
  26. cout<<"Elements in the array: ";
  27. PrintArray(arr, 0, num);
  28.  
  29. return 0;
  30. }
  31.  
  32. // Printing array recursively within a given range.
  33.  
  34. void PrintArray(int arr[], int start, int len)
  35. {
  36. // Recursion base condition
  37. if(start >= len)
  38. return;
  39.  
  40. // Printing the current array element
  41. cout<<arr[start]<<"\t";
  42.  
  43. // Recursively calling printArray to print next element in the array
  44. PrintArray(arr, start + 1, len);
  45. }

Result

C++ language techstudy.org

     





Related Topics




Popular Posts