C Program to Add Digits of a Number using Recursion
Write a Program to Add Digits of a Number using Recursion
// C Program to Add Digits of a Number using Recursion #include <stdio.h> int add_digit(long int n) { int sum = 0; if (n == 0) { return n; } sum = add_digit(n / 10) + (n % 10); return sum; } int main() { long int n; printf("Enter Number :--> "); scanf("%ld", &n); int result = add_digit(n); printf("The sum of the digits is : %d", result); return 0; }
Output:
Enter Number :--> 654321
The sum of the digits is : 21