Q:

Write C program to print elements of array using recursionon

0

Write C program to print elements of array using recursionon

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

#include <stdio.h>
#define MAX_SIZE 100
 
//Function declaration
void PrintArray(int arr[], int start, int len);
 
 
int main()
{
    int arr[MAX_SIZE];
    int num, i;
 
    // Inputting size and elements in array
    printf("Enter size of the array: ");
    scanf("%d", &num);
    printf("Enter elements in the array: ");
    for(i=0; i<num; i++)
    {
        scanf("%d", &arr[i]);
    }
 
    // Printing array recursively
    printf("Elements in the array: ");
    PrintArray(arr, 0, num);
 
    return 0;
}
 
// Printing array recursively within a given range.
 
void PrintArray(int arr[], int start, int len)
{
    // Recursion base condition
    if(start >= len)
        return;
 
    // Printing the current array element
    printf("%d ", arr[start]);
 
    // Recursively calling printArray to print next element in the array
    PrintArray(arr, start + 1, len);
}

Result:

Enter size of the array: 5

Enter elements in the array: 10

20

30

40

50

Elements in the array: 10 20 30 40 50

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write C program to find sum of array elements usin... >>
<< Write C program to find HCF or GCD of two numbers ...