Learn about C program to find the sum of digits in a number using recursion in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
C program to find the sum of digits in a number using recursion
Program:
#include<stdio.h>
int sum = 0;
int digitalSum(int n)
{
if(n!=0)
{
sum = sum + (n%10);
digitalSum(n/10);
}
return sum;
}
int main()
{
int n;
printf("Enter an integer number: ");
scanf("%d",&n);
printf("Sum of digits of %d is = %d", n, digitalSum(n) );
return 0;
}
Output:
Enter an integer number: 345
Sum of digits of 345 is = 12
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
C program to find the power of a number using a recursive function
C program to find the sum of the natural number using recursion techniques