find the sum of an array using recursion methods in C++ program
Input and Output:-
The Sum of array is using recursion 15
Program:
#include <iostream>
using namespace std;
int sumOfArray(int *arr, int n){
if(n==0)return 0;
int ans=(sumOfArray(arr, n-1)+arr[n-1]);
return ans;
}
int main() {
int arr[]={1,2,3,4,5};
int n=sizeof(arr)/sizeof(int);
cout<<"The Sum of array is using recursion "<<sumOfArray(arr, n)<<endl;
}
0 Comments