C Program to find sum of digits of a number using recursion

Simple C program to find sum of digits of a number using recursion: To find sum of digits, We have to add all digits one by one. To get digits one by one we have to find remainder of a number when we divide it by 10 and we will add all remainders and will get sum of digits. So if number is 121212 then program will print 9. If number is 12345 then 15.
#include<stdio.h>
int SumOfDigits(int n)
{
 if(n==0)
 return 0;
 return n%10+SumOfDigits(n/10);
}
int main()
{
 int n;
 printf("Enter a number: ");
 scanf("%d",&n);
 printf("Sum of digits = %d",SumOfDigits(n));
 return 0;
}






Popular posts from this blog