pysolver33
pysolver33

Reputation: 327

i don't quite understand the syntax where it adds the sum of 5 digits input

i have been reading the discussion codes and don't understand the syntax in which the sum of 5 digits is outputted. i thought C reads everything top down, left right so how is sum both % 10 and /10 syntactically?

this is a challenge from hackerrank, input 5 digits and output sum of 5 digits.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

int n;
scanf("%d", &n);
int sum = 0;

while(n)
{
    sum += n % 10;
    n/= 10;
}

printf("%d\n", sum);
//Complete the code to calculate the sum of the five digits on n.
return 0;
}

sample input 10564 sampe output 16

Upvotes: 0

Views: 88

Answers (5)

molbdnilo
molbdnilo

Reputation: 66371

The sum is not "both % 10 and /10 syntactically".

n % 10 is the least significant (rightmost) digit of n.
This is what gets added to sum.

Then n /= 10 (n = n / 10) "chops off" that digit from n, and the process repeats.

You can work through it by hand:

Set n = 12345 and sum = 0.

12345 is not zero, so enter the loop:

12345 % 10 is 5, so sum += 5, which makes 5.
12345 / 10 is 1234 , so n = 1234.

Now, n is 1234, which is not zero.

1234 % 10 is 4, so sum += 4, which makes 9.
1234 / 10 is 123, so n = 123.

123 is not zero...

And so on, until n becomes zero, which will happen after there is only one digit left.

Upvotes: 1

user3629249
user3629249

Reputation: 16540

the following description of what is happening in you code should help you to understand

#include <stdio.h>
// note, it is poor programming practice
//       to include header files those contents are not used
//#include <string.h>
//#include <math.h>
//#include <stdlib.h>

int main() 
{
    // assume input is 54321
    int n;
    scanf("%d", &n);
    int sum = 0;

    while(n)
    {
        // values at each loop
        sum += n % 10;     // remember n%10 means the remainder of n/10
            // n%10 =1 sum = 1 
            // n%10 =2 sum = 3
            // n%10 =3 sum = 6
            // n%10 =4 sum = 10
            // n%10 =5 sum = 15
        n/= 10;  // shorthand for: n = n/10
            //n = 5432
            //n = 543
            //n = 54
            //n = 5
            //n = 0
    }

    printf("%d\n", sum);
    //Complete the code to calculate the sum of the five digits on n.
    return 0;
}

the output is:

15

Upvotes: 0

Sovak
Sovak

Reputation: 22

Well, the syntax and code seems correct. The syntax 'sum += n % 10' could also be written as 'sum = sum + n%10'. And 'n/=10' as 'n=n/10'. This will support that c reads everything from top to bottom and from left to right.

Upvotes: 0

Luc A
Luc A

Reputation: 98

How it works :

While n isn't null, you add the rest of the division n/10. If n = 10564 : At the first iteration, you get 4, the result of 10564%10. Then, by dividing 10564 by 10 you get 1056,4. BUT n is an integer value, so you get n = 1056

By doing the same operation, you always get the units of n and add it to the sum.

Upvotes: 0

Yoni Newman
Yoni Newman

Reputation: 195

Altough the compiler can make some optimizations so your code might change, C does work on your code top down.

The thing that happends here in the while loop

sum += n % 10; == sum = sum + n % 10; first of all, % operator is activate, and then the + operator. So, first you get the last significant digit of the number, and then you sum it up to your result and assign it into sum.

the same thing happens in n/= 10;. first you divide the number that saved in n and then you store the result in n.

I hope it answers your question.

Upvotes: 0

Related Questions