Sum of Digits of a Number in C

This article explores the different methods to calculate the sum of digits of a number in C. This article includes not only the algorithm for the same but also different methods like the iterative as well as the recursive way to calculate the sum of the digits of a number in C. The algorithm and code in case the number is given in string format have also been discussed.
Algorithm to Find the Sum of Digits
The basic idea when finding the sum of all digits of a number is just to find the value of each digit that forms the number and add them. Let us look at the algorithm that can be used to find the sum of digits for a given number.
Assume we have number num.
START
- Step 1 -> Initialize a variable sum = 0 to count the sum of all digits for num
- Step 2 -> Start a while loop with the condition that num > 0.
- Step 3 -> Add to sum the value at ones place in num as sum = sum + num%10. Here, num%10 represents the value of the digit at the ones placed in num.
- Step 4 -> Divide num by 10 as the current digit at one place has been counted.
STOP
The logic behind the algorithm is that we have to just take the value at each digit and add this value to the sum. For this, we take the value at one's place in the number by taking its modulo with 10 and after that, we divide the number by 10 so that we can count the value of all other digits as well.
Time Complexity
The time complexity of this algorithm is O(log(n)). This is because to calculate the sum of all digits of a number we need the values of all digits and finding that will take O(log(n)) time.
Various Methods to Find the Sum of Digits in C
Let us now look at the different ways by which we can find the sum of digits of a number in C.
Method 1: The Iterative Way of Calculation
We can use a while loop to find the sum of all digits of a number in C. Let us take a look at the code and then understand the logic behind it.
Output
In the above code, we run a while loop till our given number is not equal to 0. This is because to find the sum of all digits we keep adding the value at the one's place in the number and then dividing it by 10 as the number at one's place has been counted.
Method 2: A Recursive Approach
We already saw how we can use an iterative approach using a while loop to calculate the sum of digits of a number in C. Let us now look at another approach, i.e. the recursive way to count the sum of digits of a number in C. Let us take a look at the code and then understand the logic behind it.
Output
In the above code, the recursive function has the base case when the number is 0 as no more digits are left and we simply return the sum calculated till now. Otherwise, we add the value of the digit at one place to the sum and recurse again for the number/10 as the digit at one place has been counted.
Method 3: A Reduced Down Recursive Approach
We already saw the recursive approach to finding the sum of digits of a number in C, but we can further simplify the approach. Let us look at the code and then compare the two solutions.
Output
The only difference between this code and the previous code is that instead of keeping a sum variable to count the sum of digits, we are just returning the individual digits without storing them in a variable in the recursive function.
Method 4: Taking Numbers As Input in Char Array/String Format
In all the previous methods, we took the number as an integer. This works well, but when we have to find the sum of digits of very large numbers, like the order of 10^19 or more, these cannot be represented in integer format. In such a case, we will have to represent the integer in a character array or string format in C. Let us take a look at the code of how we can find the sum of digits of such a large number as a character array or string in C and then look at the logic behind it.
Output
In the above code, we have followed the iterative approach. Basically, we iterate over the character array using a for loop and as we have the digits stored as characters, we will have to convert their ASCII value into their decimal value. For this reason, we subtract each character with 48, which is the ASCII value of character 0. Hence, we get the decimal value of each of the digits which we add to the sum.
Note: ASCII stands for American Standard Code for Information Interchange. It is a character encoding format where different characters are assigned a code.
Method 5: Working with Character Input
There is another method of calculating the sum of digits of a number given to us as a character array or string in C. Let us look at the code first.
Output
This code is the same as the above-discussed code, except that instead of subtracting the ASCII value of the character 0, we subtract the character 0 itself from the character digits. As we are then assigning this value to an integer variable, it is automatically converted into the decimal value of the current digit, then we just add this value to the total sum.
Conclusion
- The various methods to calculate the sum of digits of a number in C are:
- Iterative approach: Using a while loop
- Recursive approach: Using a recursive function
- Reduced down recursive approach: Using recursive function without sum variable
- When the input is in string format: Suitable for large numbers
- The time complexity to find the sum of digits of a number in C is O(log(n)) where n is the number.