- Write a program in C to find sum of digits of a number in single statement.
- How to find the sum of digits of a number in one statement using recursion.

**Algorithm to find sum of digits of a number**

- Get least significant digit of number (number%10) and add it to the sum variable.
- Remove least significant digit form number (number = number/10).
- Repeat above two steps, till number is not equal to zero.

## C program to find sum of digits of a number using for loop

# include<stdio.h> int main() { int n, sum; printf("Enter a number\n"); scanf("%d", &n); for(sum=0; n > 0; sum += n%10, n/=10); printf("Sum of digits : %d", sum); return 0; }Output

Enter a number 1234 Sum of digits : 10

## C program to find sum of digits of a number using recursion

#include<stdio.h> int sumOfDigits(int n) { return n == 0 ? 0 : n%10 + sumOfDigits(n/10) ; } int main() { int n, sum; printf("Enter a number\n"); scanf("%d", &n); printf("Sum of Digits : %d", sumOfDigits(n)); return 0; }Output

Enter a number 12345 Sum of Digits : 15