Write C program to print elements of array using recursionon


Introduction

I have used Code::blocks 12 compiler for debugging purpose. But you can use any C programming language compiler as per your availability.


techstudy.org

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

Write C program to print elements of array using recursionon


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

Result

C language

     





Related Topics




Popular Posts