C Program to Find Sum of Digits of a Number using Recursion

To find the sum of digits of a number, we have to add the each digit of a number. Here, to find the sum of digits of a number we will remove one digit at a time from a number and add it to a variable.

For Example
Sum of digits of 4265 = 4 + 2 + 6 + 5 = 17

To extract digits of a number we can use ‘/'(division) and ‘%'(modulus) operator. Number%10 will give the least significant digit of the Number, we will use it to get one digit of number at a time. To remove last least significant digit from number we will divide number by 10. We can use recursion to implement above mentioned algorithms because it can be expressed as recursive equation.

Let getSum(N) returns the sum of digits of N. We can express sum of digits of N recursively as :
getSum(N) = N%10 + getSum(N/10)

For Example
getSum(1234) = 1234%10 + getSum(1234/10) = 4 + getSum(123).

Algorithm to find the sum of digits of number using recursion
Let N be the input number.

  • N%10 gives the least significant digit of N(rightmost digit of N). For Example: 2345%10 = 5.
  • N/10 return the number after removing least significant digit of N(rightmost digit of N). For Example: 2345/10 = 234.

C program to calculate sum of digits of a number recursively

Below program user a user defined function getSumOfDigit, that takes an integer as input parameter(num) and return the it’s sum of digits. This function implements the above mentioned recursive algorithm using division and modulus operator. Recursion will terminate when num becomes zero.

C Program to Find Sum of Digits of a Number using Recursion

/*
* C Program to print sum of digits of a number using number
*/
#include <stdio.h>
#include <conio.h>
 
int main(){
    int num;
    printf("Enter a number \n");
    scanf("%d", &num);
    printf("Sum of digits of %d is %d\n", num, getSumOfDigit(num));
    getch();
    return 0;
}
 
/*
 * Function to calculate sum of digits of a number
 */
int getSumOfDigit(int num){
    /* Recursion termination condition*/
    if(num == 0)
        return 0;
                
    return num%10 + getSumOfDigit(num/10);    
}

Program Output

Enter a number 
3426
Sum of digits of 3426 is 15

C program to calculate sum of digits of a number using loop

Below program calculates the sum of digits of a number using while loop. It first takes a number as input from user using using scanf function and stores it in an integer variable. In line number 14, it extract the least significant digit of number and adds it to the variable digitSum. In line number 17, it removes the least significant digit from number. Inside the while loop, above process continues until number become zero.

C program to calculate sum of digits of a number using loop

/*
* C Program to find sum of digits of a number
*/
#include <stdio.h>
#include <conio.h>
  
int main(){
    int number, digitSum = 0;
    printf("Enter a number : ");
    scanf("%d", &number);
    while(number != 0){
        /* get the least significant digit(last digit)
         of number and add it to digitSum */
        digitSum += number % 10;
        /* remove least significant digit(last digit)
         form number */
        number = number/10;
    }    
    printf("Sum of digits : %d\n", digitSum);
    getch();
    return 0;
}

Program Output

Enter a number : 12345
Sum of digits : 15