Tech Study

Write C++ program to find sum of array elements using recursion

Introduction

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

#include <iostream>
using namespace std;
 
#define MAX_SIZE 100
 
// Function declaration
int sum(int arr[], int start, int len);
 
 
int main()
{
    int arr[MAX_SIZE];
    int num, i, sumofarray;
 
 
    // Inputtin size and elements in array
    cout<<"Enter size of the array: ";
    cin>>num;
    cout<<"Enter elements in the array: ";
    for(i=0; i<num; i++)
    {
        cin>>arr[i];
    }
 
 
    sumofarray = sum(arr, 0, num);
    cout<<"Sum of array elements: "<<sumofarray;
 
    return 0;
}
 
// Recursively finding the sum of elements in an array.
int sum(int arr[], int start, int len)
{
    // Recursion base condition
    if(start >= len)
        return 0;
 
    return (arr[start] + sum(arr, start + 1, len));
}

Result

Write C++ program to find sum of array elements using recursion
Write C++ program to find sum of array elements using recursion

TaggedWrite C program to find sum of array elements using recursion

Python Examples

Introduction: Python Examples are the basic programming concepts of python like python syntax,python data types,,python operators,python if else,python comments etc.. …

Read more

C String Functions

C String Functions perform certain operations, It provides many useful string functions which can come into action. The <string.h> header …

Read more