How to master recursion in c/c++..
These are 2 different codes: 1st by me and 2nd by GFG. Both calculates the sum of an array but mine is different and maybe poor.... Mine one: #include<stdio.h> int a[5]={1,2,6,8,9}; int main() { int x; x=s(4); printf("sum is %d",x); return 0; } int s(int i) { static sum; if(i==0) { sum=sum+a[i]; //return sum; } else{ sum=sum+a[i]; i--; s(i); } return sum; } By GFG: #include <stdio.h> int findSum(int A[], int N) { Â Â Â Â if (N <= 0) Â Â Â Â Â Â Â Â return 0; Â Â Â Â return (findSum(A, N - 1) + A[N - 1]); } int main() { Â Â Â Â int A[] = { 1, 2, 3, 4, 5 }; Â Â Â Â int N = sizeof(A) / sizeof(A[0]); Â Â Â Â printf("%dn", findSum(A, N)); Â Â Â Â return 0; } Which one is more appropriate and easier to explain?? Im not sure that does really my code is following recursion or not. Can u explain/guide me?. Give some tips to master recursions also.