**Sum of digits in a C** program allows a user to enter any number, divide that number into individual numbers, and sum those individual numbers.

**Example 1:**

Given number = **14892** => 1 + 4 + 8 + 9 + 2 = 24.

Sum of digits of a given number “**14892**” is **24**.

**Example 2:**

Given number = **3721** => 3 + 7 + 2 + 1 = 13.

Sum of digits of a given number “**3721**” is **13**.

Write a C program to take the number from user and find the sum of its digits.

**Sum of Digits Algorithm in C:**

1. Take the number as input.

2. Divide the number by 10 and store the remainder in a variable.

3. Add the remainder to the sum.

4. Repeat the process until the number is not 0.

5. Print the sum.

There are several ways to find the sum of digits in C language. Let’s take a detailed look at all the approaches for finding the sum of digits in C.

- Sum of Digits in C using While Loop (Naive Approach)
- Sum of Digits in C using Recursive Approach
- Sum of Digits in C using Separate Function (Advanced Approach)
- Read a String and Find the Sum of all Digits in the String

In this approach, we will divide the number by 10 and add the remainder to the sum.

**Examples:**

**Input:** 123 **Output:** 6

**Input:** 8679 **Output:** 30

Here is source code of the C program to compute the sum of digits in a given integer. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/* C program to find sum of digits of a number */`

`#include <stdio.h>`

int main(void)

`{`

int num, sum = 0, rem;

printf("Enter a number: ");

scanf("%d", &num);

`// Keep dividing until the number is not zero`

while (num != 0)

`{`

rem = num % 10;

sum = sum + rem;

num = num / 10;

`}`

printf("Sum of digits of the number is %d", sum);

return 0;

`}`

1. Take an integer as a input and store it in the variable **num**.

2. Initialize the variable **sum** to zero.

3. Divide the input integer by 10 and obtain its remainder & quotient.

4. Store the remainder in the variable **rem**.

5. Increment the variable **sum** with variable **rem**.

6. Store the quotient into the variable **num**.

7. Repeats the process until the number is not 0.

8. Print the variable sum as output and exit.

**Time Complexity: O(n)**

Time complexity of the above algorithm is O(n) as the while loop runs for log(n) times because at each iteration the number is divided by 10, where n is the number given as input.

**Space Complexity: O(1)**

In the above program, space complexity is O(1) as no extra variable is taken to store the value. All the variables initialized takes constant O(1) space.

**Testcase 1:** In this case, we are entering the number “123” as input.

Enter a number: 123 Sum of digits of the number is 6

**Testcase 2:** In this case, we are entering the number “8679” as input.

Enter a number: 8679 Sum of digits of the number is 30

In this approach, we use a recursive function to calculate the sum of digits.

**Methods used:**

**long sum_of_digits_recur(long num)** – This function calculates the sum of digits of a number recursively.

**Approach to Find the Sum of Digits using Recursion:**

1. Take the number as input.

2. Call the function **sum_of_digits_recur** to calculate the sum of digits, the procedure is similar to whatever we did before with the difference that now it is recursive and therefore needs a few changes and a termination condition.

3. Print the sum.

**Example:**

**Input:** 3248162424

**Output:** 36

**Input:** 32471844421

**Output:** 40

Here is source code of the C program to find the sum of digits using recursion. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/* Find the sum of digits recursively */`

`#include <stdio.h>`

long sum_of_digits_recur(long n)

`{`

if (n == 0)

return 0;

`else`

return n % 10 + sum_of_digits_recur(n / 10);

`}`

int main(void)

`{`

long n;

printf("Enter a number: ");

scanf("%ld", &n);

printf("Sum of digits of the number is %ld", sum_of_digits_recur(n));

return 0;

`}`

1. Take an integer as a input and store it in the variable **n**.

2. Call the function **sum_of_digits_recur** to calculate the sum of digits.

3. Inside the function, we write a temrinating condition which is whenever the number becomes 0, the program should return back 0.

4. Otherwise, the program should add the remainder along with the number divided by 10 which is turn sent back to the function.

5. At the end, all sums are added up and the result is displayed to the user.

**Time Complexity: O(n)**

Time complexity of the above program is O(n), where n is the number given as input.

**Space Complexity: O(n)**

In the above program, space complexity is O(n).

**Testcase 1:** In this case, we are entering the number “3248162424” as input.

Enter a number: 3248162424 Sum of digits of the number is 36

**Testcase 2:** In this case, we are entering the number “32471844421” as input.

Enter a number: 32471844421 Sum of digits of the number is 40

In this approach, we use a separate function to calculate the sum of digits.

**Methods used:**

**long sum_of_digits(long num)** – This function calculates the sum of digits of a number.

**Approach:**

1. Take the number as input.

2. Call the function **sum_of_digits** to calculate the sum of digits, the procedure is similar to whatever we did before.

3. Print the sum.

**Examples:**

**Input:** 14839481349

**Output:** 54

**Input:** 127381723

**Output:** 34

Here is source code of the C program to find the sum of digits using seperate function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/* Find the sum of digits of a number using a separate function */`

`#include <stdio.h>`

long sum_of_digits(long n)

`{`

long sum = 0;

while (n > 0)

`{`

sum += n % 10;

n /= 10;

`}`

return sum;

`}`

int main(void)

`{`

long n;

printf("Enter a number: ");

scanf("%ld", &n);

printf("Sum of digits of the number is %ld", sum_of_digits(n));

return 0;

`}`

1. Take an integer as a input and store it in the variable **n**.

2. Call the function **sum_of_digits_recur** to calculate the sum of digits.

3. Inside the function, we write a temrinating condition which is whenever the number becomes 0, the program should return back 0.

4. Otherwise, the program should add the remainder along with the number divided by 10 which is turn sent back to the function.

5. Finally, print the **sum**.

Suppose the user enters the number **14839481349**. In the printf statement, we call the function **sum_of_digits** with the number **14839481349** as the argument. Inside the function, a while loop executes until the number is not 0. The loop adds the remainder to the **sum**.

**Time Complexity: O(n)**

Time complexity of the above program is O(n), where n is the number given as input.

**Space Complexity: O(n)**

In the above program, space complexity is O(1) as no extra variable is taken to store the value. All the variables initialized takes constant O(1) space.

**Testcase 1:** In this case, we are entering the number “14839481349” as input.

Enter a number: 14839481349 Sum of digits of the number is 54

**Testcase 2:** In this case, we are entering the number “127381723” as input.

Enter a number: 127381723 Sum of digits of the number is 34

This C program will find the sum of digits of a number taken as a string.

**Methods used:**

**int sum(char*)** – This function will find the sum of digits of the string.

**Approach:**

1. Take the string as input.

2. Store the string in a variable.

3. Inside for loop, take the string and extract every character and while finding its value as a digit, add it to the sum.

4. If the character is not a digit, then we ignore it.

5. If the character is a digit, then we add it to the sum.

6. Return the sum.

7. Print the sum.

**Examples:**

**Input:** 82346y8d83 **Output:** 32

**Input:** de29382h **Output:** 24

Here is source code of the C program to find the sum of digits of a number taken as a string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

`/* C program to find sum of digits of a number taken as a string */`

`#include <stdio.h>`

`#include <string.h>`

int sum(char *str)

`{`

int sum = 0;

size_t i;

size_t l = strlen(str);

for (i = 0; i < l; i++)

`{`

if (str[i] >= '0' && str[i] <= '9')

`{`

sum = sum + (str[i] - '0');

`}`

`}`

return sum;

`}`

int main(void)

`{`

char str[100];

printf("Enter the string: ");

scanf("%s", str);

printf("The sum of digits of numbers in the string is: %d", sum(str));

return 0;

`}`

1. The program begins with asking the user to enter the string.

2. The string is stored in a variable **str** and then passed to the function **sum()**.

3. In the function **sum()**, we begin with initializing the variable **sum** to 0.

4. Then extract every character from the string and while finding its value as a digit, add it to the sum.

5. If the character is not a digit, then ignore it. If the character is a digit, then add it to the sum and finally return the **sum**.

**Testcase 1:** In this case, we are entering the number “82346y8d83” as input.

Enter a number: 82346y8d83 Sum of digits of the number is 32

**Testcase 2:** In this case, we are entering the number “de29382h” as input.

Enter a number: de29382h Sum of digits of the number is 24

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

**If you find any mistake above, kindly email to [email protected]**

**Related Posts:**

- Practice Computer Science MCQs
- Apply for C Internship
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Check Computer Science Books