c – Signed integer overflow: 999999999 * 10 cannot be represented in type ‘int’ Error

There are (two) major problems with your code, which both lead to the same error:
The int length variable is being increased to a point where it reaches the max values.

Here is you while loop:

while (NUM > range1)
    long long range2 = range1 * 10 + 9;
    length += 1;

Problem #1

This biggest issue is that this while loop will never end. You never change the values of NUM or range1 in the while loop, so if NUM starts out as greater than range1, you will get stuck in an infinite loop. length += 1 will keep getting called until the length integer reaches the max allowed int value.

(Possible) Problem #2

Depending on how you fix Problem #1, you can also face the following issue.

As you stated in a comment above, NUM is a credit card number.

  • Credit card number by default are
    16 digits long.
  • The max value of and int variable is 999999999 *
    10 which is max 10 digits.

If your loop is set to run the value of NUM times until it reaches 9. If we let’s say pick the lowest possible 16 digit credit card number, 1000000000000000, your loop still will still run 1000000000000000 - 9 times.

Every time your loop runs, length is increased by 1. The loop will basically try to increase the int variable at least 999999999999991 times which will cause the value to become greater than 999999999 * 10.

(Possible) Fix

There are not enough details in your question to know if for sure this is what will solve your problem, but I will guess that instead of

long long range2 = range1 * 10 + 9;

you probably meant to write

range1 = range1 * 10 + 9;

Read more here: Source link