# Why is my C modulo code giving repeating identical output?

I am wondering why my output repeats before it gets to the final output:

``````#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void)
{
long bignum = 4300123488893756;
long modulod = 0;
long countthou = 10;
while(countthou < 10000000000000001)
{
modulod = bignum % countthou;
printf("modulod at %lu is %lu\n", countthou, modulod);
countthou = countthou * 10;
}
}
``````

And the output is:

``````modulod at 10 is 6
modulod at 100 is 56
modulod at 1000 is 756
modulod at 10000 is 3756
modulod at 100000 is 93756
modulod at 1000000 is 893756
modulod at 10000000 is 8893756
modulod at 100000000 is 88893756
modulod at 1000000000 is 488893756
modulod at 10000000000 is 3488893756
modulod at 100000000000 is 23488893756
**modulod at 1000000000000 is 123488893756
modulod at 10000000000000 is 123488893756
modulod at 100000000000000 is 123488893756**
modulod at 1000000000000000 is 300123488893756
modulod at 10000000000000000 is 4300123488893756
``````

Why do those three lines have the same modulo?

Remember what the modulo operation does. It returns the remainder of x % y. Each pass through the loop multiplies y * 10.

Now, look at the numbers for the three identical results. The first one is the remainder of an integer divide, as you expected. But the next two digits in x, used in the next two loops, produce the same result. Let me both demonstrate it and simplify it a little:

``````3002 % 10 = 2
3002 % 100 = 02 = 2
3002 % 1000 = 002 = 2
3002 % 10000 = 3002
``````

Anytime a 0 is encountered in a number that is modulo'd by a power of 10, it will produce the same result as the previous loop.

So, it's actually working correctly.

Questions? ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

• Thanks for the help! Off to study more. Commented Jun 10, 2021 at 11:43