A
A
Anvario02021-12-29 17:41:49
C++ / C#
Anvario0, 2021-12-29 17:41:49

Why doesn't a C program handle numbers greater than 1999999996?

I wrote a program that increases the power of 10 until a condition breaks the loop.
my code:

int n = 0;
  while (1) {
    long long int i = 2 * pow(10, n) - 4;
    if (i % 19 == 0)
    {
      printf("%d", n);
      break;
    }
    printf("\t %d\n", i);
    n++;
  }

But here is what the console issues :
-2
16
196
1996
199999999999999999999999999999999999999999999999999999999836484
-1863462916
-1454759940
-1662697476
55289463343334
1311111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111AR
_
_
_
_
_ What is the problem? Why doesn't the program process numbers greater than 1999999996?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2021-12-29
@Rsa97

Because in the formula you have all values ​​of type int, respectively, and the calculations go to int, not long.

long long n10 = 10;
while (1) {
    if ((n10 * 2 - 4) % 19 == 0) {
      printf("%ld", (n10 * 2 - 4) / 19 * 10 + 2);
      break;
    }
    n10 *= 10;
}

G
GavriKos, 2021-12-29
@GavriKos

Most likely in print. %d can't stomach that much

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question