Reputation: 83
Why is there a runtime error? I made range2
a long long.
Code:
/*VARIABLES FOR WHILE LOOP*/
long long range1 = 9;
int length = 1;
/*FIND NUM'S LENGTH*/
while (NUM > range1)
{
long long range2 = range1 * 10 + 9;
length += 1;
}
Error:
credit.c:25:25: runtime error: signed integer overflow: 999999999 * 10 cannot be represented in type 'int'
Upvotes: 7
Views: 56230
Reputation: 1
You have to carefully read the question because in some question input/output is signed 32-bit integer or unsigned 32-bit integer both have a different range so value outside the range is invalid or you have to return 0 just like bellow e.g. Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned). Solution
Upvotes: 0
Reputation: 1
Size of unsigned int is (0 to 4,294,967,295) for 4 bytes;
So 999999999 *10 is greater than 4,294,967,295
therefore try using (long)
long long int x=999999999 *10 (try this...)
Upvotes: -1
Reputation: 586
You can try this one to handle the above conditions
With the two most common representations, the range is 0 through 4,294,967,295 (2^32 − 1) for representation as an (unsigned) binary number, and −2,147,483,648 (−2^31) through 2,147,483,647 (2^31 − 1) for representation as two's complement
I'm recently faced it while solving a question having constraints
Given a 32-bit signed integer, reverse digits of an integer
runtime error: signed integer overflow: 999999999 * 10 cannot be represented in type 'int'
So, here is my code
while (x != 0) {
int pop = x % 10;
x /= 10;
if (rev > INT_MAX/10 || (rev == INT_MAX / 10 && pop > 7)) return 0;
if (rev < INT_MIN/10 || (rev == INT_MIN / 10 && pop < -8)) return 0;
rev = rev * 10 + pop;
}
before I'm getting 999999999*10, rev>INT_MAX it guaranteed OVERFLOW & vice-versa.
Upvotes: 4
Reputation: 8766
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;
}
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.
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.
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.
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;
Upvotes: 2