In this program, we are going to share a C program to calculate the sum of all digits using recursion. If you are a beginner and want to start learning the C programming, then keep your close attention in this tutorial as I am going to share a C program to calculate the sum of all digits using recursion with the output.
We have designed this program for beginners for learning purpose. Copy below c program and execute it with c compiler to see the output of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <stdio.h> int sumDigits(int num) { static int sum=0; if(num>0) { sum+=(num%10); sumDigits(num/10); } else { return sum; } } int main() { int number,sum; printf("Enter a positive integer number: "); scanf("%d",&number); sum=sumDigits(number); printf("Sum of all digits are: %d\n",sum); return 0; } |
Enter a positive integer number: 1230
Sum of all digits are: 6
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.