Program 280: Print Sum of Even Numbers in Array using Recursion
Output:
#include<stdio.h> void SumOfEven(int a[],int num,int sum); main() { int i,a[100],num,sum=0; printf("Enter number of Array Elements\n"); scanf("%d",&num); printf("Enter Array Elements\n"); for(i=0;i<num;i++) { scanf("%d",&a[i]); } SumOfEven(a,num-1,sum); } void SumOfEven(int a[],int num,int sum) { if(num>=0) { if((a[num])%2==0) { sum+=(a[num]); } SumOfEven(a,num-1,sum); } else { printf("Sum=%d\n",sum); return; } }Explanation:
- This program starts with initializing :
- i→ Helping variable
- a[100[→To sore numbers entered by user
- num→ Total number of elements n array
void SumOfEven(int a[],int num,int sum);→Declaring function
- sum→ To store sum of the even numbers
printf("Enter number of Array Elements\n"); scanf("%d",&num); printf("Enter Array Elements\n"); for(i=0;i<num;i++) { scanf("%d",&a[i]); }
Taking numbers from user and storing in aSumOfEven(a,num-1,sum);
Calling SumOfEven Function where we are passing array a ,total number of elements as num-1 because we have to calculate from 0 to n-1 as array starts with 0 so if we give 7 elements in array then recursion will execute 7 times i.e from 0-6, and sum which is initialized to zerovoid SumOfEven(int a[],int num,int sum) { if(num>=0) { if((a[num])%2==0) { sum+=(a[num]); } SumOfEven(a,num-1,sum); } else { printf("Sum=%d\n",sum); return; } }
Now lets take 4 elements in array 30,35,40,45 so sum should be 30+40=70.We passed this array to SumOfEven so num=(4-1)=3 and sum=0 this is our initial data- Now
if(num>=0) { if((a[num])%2==0) { sum+=(a[num]); } SumOfEven(a,num-1,sum); }
Since num=3 which is >=0 so if part will execute.- a[num]=a[3]=45
- a[num]%2=45%2!=0. So sum+=a[num] will not execute
- Finally sum=0
- Now we are calling SumOfEven(a,num-1,sum) again
- So we are passing SumOfEven(a,3-1,0) ---->SumOfEven(a,2,0)
- The same process in step 1 continue again
if(num>=0) { if((a[num])%2==0) { sum+=(a[num]); } SumOfEven(a,num-1,sum); }
Since num=2 which is >=0 so if part will execute.- a[num]=a[2]=40
- a[num]%2=40%2==0. So sum+=a[num] will execute
- sum=0+40=40
- Finally sum=40 in recursion 2
- Now we are calling SumOfEven(a,num-1,sum) again
- So we are passing SumOfEven(a,2-1,40) ---->SumOfEven(a,1,40)
- Since num=1 which is >=0 so if part will execute.
- a[num]=a[1]=35
- a[num]%2=35%2!=0. So sum+=a[num] will not execute
- Finally sum=40 in recursion 3 and no change is there
- Now we are calling SumOfEven(a,num-1,sum) again
- So we are passing SumOfEven(a,1-1,40) ---->SumOfEven(a,0,40)
- Since num=0 which is >=0 so if part will execute.
- a[num]=a[0]=30
- a[num]%2=30%2==0. So sum+=a[num] will execute
- sum=40+30=70
- Finally sum=70 in recursion 4
- Now we are calling SumOfEven(a,num-1,sum) again
- So we are passing SumOfEven(a,0-1,70) ---->SumOfEven(a,-1,70)
- Since num=-1 which is not >=0 so else part will execute.
else { printf("Sum=%d\n",sum); return; }
- So here sum will be printed which is 70
- And then it returns back to main()
Output: