Menu ↓

The Sum of Digit Number in C Language

When I'm working on this HackerRank Problem which is about how to sum a certain digit number

I feel Déjà vu.

I remember encountering the same problem during an online coding interview when applying for a job application back then. Of course, I fail at that time.

The Past

I forgot when I encountered this problem and which company I applied to back then. But, I couldn't forget the problem even until today. Because I still remember how hilarious my solution was back then.

If my memory is correct, this is my approach to tackling the problem:

SET result to 0
CONVERT the n from Integer to String
CONVERT the n from String to Char Array
FOR each character from the n
		CONVERT the character to an Integer
		SUM the result with the current digit
END FOR

I'm using Java as the programming language. My idea back then is to iterate each of the digit numbers and then sum it. But, with a million conversions.

First, to get each digit from the given number I convert from integer to String and lastly to an array of characters..

I know, it's not the best solution. But, it should solve the given problem. Alas, my proposed solution was not accepted. Heck, even the platform gave me this message:

The solutions do not meet the expected performance/complexity please find another solution.

I forgot what is exactly the message, but that's the gist. Of course, I failed.

The Present

Never in my wildest dreams, did I find a similar problem again. No less in my journey to learn about the C programming language offered by HackerRank.

Since this is part of the learning about C programming language. Of course, the problem description gives a hint on how to solve the problem. Which I couldn't understand at all.

The modulo operator, %, returns the remainder of a division. For example, 4 % 3 = 1 and 12 % 10 = 2. The ordinary division operator, /, returns a truncated integer value when performed on integers. For example, 5 / 3 = 1. To get the last digit of a number in base 10, use 10 as the modulo divisor.

I mean, isn't the modulo useful only for deciding whether a number is odd or even?

Well… I still couldn't figure how to solve the problem. So, I employ my Google-fu and it's led me to this.

This is the code after I modify it to adjust with the Hacker Rank problem:

int main() {
    int n;
    scanf("%d", &n);
    //Complete the code to calculate the sum of the five digits on n.
    int sum = 0;
    while (n != 0) {
       sum += n % 10;
       n = n / 10;
    }
    printf("%d\n", sum);
    return 0;

Well, it works. Now, what I need is to understand how this algorithm could solve the problem.

It took me days to finally understand how it works. But, after realising the keyword the puzzle is solved now. The keyword is this:

To get the last digit of a number in base 10, use 10 as the modulo divisor.

To be honest, I do some cheat to understand the algorithm. I put some printf here and there and this is the result:

Find the sum of digit number for 58612
================================
The current given number is 58612
Sum the `sum` with last digit number from given number
sum = sum + (last digit number)
sum = 0 + (58612 mod 10)
sum = 0 + 2
Now reduce the given number, let's remove the last digit number
num = 58612 / 10
num = 5861
================================
The current given number is 5861
Sum the `sum` with last digit number from given number
sum = sum + (last digit number)
sum = 2 + (5861 mod 10)
sum = 2 + 1
Now reduce the given number, let's remove the last digit number
num = 5861 / 10
num = 586
================================
The current given number is 586
Sum the `sum` with last digit number from given number
sum = sum + (last digit number)
sum = 3 + (586 mod 10)
sum = 3 + 6
Now reduce the given number, let's remove the last digit number
num = 586 / 10
num = 58
================================
The current given number is 58
Sum the `sum` with last digit number from given number
sum = sum + (last digit number)
sum = 9 + (58 mod 10)
sum = 9 + 8
Now reduce the given number, let's remove the last digit number
num = 58 / 10
num = 5
================================
The current given number is 5
Sum the `sum` with last digit number from given number
sum = sum + (last digit number)
sum = 17 + (5 mod 10)
sum = 17 + 5
Now reduce the given number, let's remove the last digit number
num = 5 / 10
num = 0
the sum: 22

Now, let's break down what's happening.

Variable Assignment

num := 58612
sum := 0

First, we want to sum the digit number which we assign to the num variable. In this case is 58612. We also declare a sum variable with a value of 0. In the end, we want the sum variable to contain a value from 5+8+6+1+2 which results in 22.

Iteration

while num is not equal to 0, do:

Next, we want to iterate for each digit number and sum it. To do that, we declare an iteration. The iteration will only stop when the num is equal to zero (0).

Well, if you pay attention the num value is 58612. So, inside of the iteration, we must do something to reduce the num until it becomes zero (0).

Main Logic

sum := sum + num mod 10
num := num / 10

Inside the iteration is where the main logic starts. In this section, there are two things. First, we start to sum each digit number from the sum. Second, we decrease the num until it becomes zero (0).

I will start with this num := num / 10 first. The purpose of this logic is to assign the num with new value from num / 10. Now, if you grab a calculator and start divide 58612 with 10 you will get 5861,2. But, since our num is an int the decimal value will be discarded and you will only get 5861 as the result.

Keep dividing the num by ten (10), and eventually the num will become zero (0). Thus, the iteration will be stopped.

Now, my cyber friend, you know that dividing the num by ten (10) will slowly remove the last digit number. Could you imagine now what the result of num % 10?

Yes, it will give you the last digit number.

So, let's break down what happening with this code sum := sum + num mod 10 on the first iteration.

sum = sum + (num % 10)
sum = 0   + (58612 % 10)
sum = 0   + (2)
sum = 2

Now, let's move to the second iteration. Remember that the num was already divided by 10 and now its value is 5681.

sum = sum + (num % 10)
sum = 2   + (5861 % 10)
sum = 2   + (1)
sum = 3

Return Value

return sum

Finally, when the iteration operation is finished. We return the sum, which already obtain new value from 2 + 1 + 6 + 8 + 5.

Today, I learned something amazing.

Menu Navigation