Home / Programs / Find Sum of Digits of a Number using Recursion
Programming Example

Find Sum of Digits of a Number using Recursion

👁 705 Views
💻 Practical Program
📘 Step by Step Learning
Find Sum of Digits of a Number using Recursion

Program Code

#include<stdio.h>
int funSum(int x);
int main(){ 
   int num = 123;
   int f;
   f = funSum(num);
   printf("sum of %d and %d ",num,f);
   return 0; 
}
int funSum(int x){
	int a; 
	int y = 0;
	if(x<=0){
		return 0;
	}
	else{
		a = x%10;
		x = x/10; 
		y = a + funSum(x);
		return y;
   } 
    
}

Output

sum of 123 and 6

Explanation

None

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.