# I'm getting a floating point exception error in the credit code but I definitely haven't divided by 0

Here is the code:

``````int sum1(long long n)
{
int sum1 = 0;
int x = 0;
for (long long i = 100; (n % i) <= n; i *= 100)
{
x = 2 * ( ( (n % i) - (n % (i / 10)) ) / (i / 10) );
sum1 += ((x % 10) + (((x % 100) - (x % 10)) / 10));
}
return sum1;
}

int sum2(long long n)
{
int sum2 = 0;
for (long long i = 10; (n % i) <= n; i *= 100)
{
sum2 += ( ( (n%i) - (n%(i/10)) ) / (i/10) );
}
return sum2;
}
``````

So these are the functions that calculate the two sums that constitute the checksum, i.e., the sum1 function first gets all alternate digits of the credit card number (n), starting from the second-to-last digit and multiplies them by 2, then adds the digits of each of these products together to give the int sum1, while the sum2 function simply adds all the remaining digits of n which weren't multiplied by 2 and gives int sum2.

The problem is probably in the for loop condition `(n % i) <= n`, which is used to check if the number has reached its first digit (since we're going backwards from the second-to-last digit). First I had used the condition `(n % i) != n` and the code was working perfectly except it obviously didn't consider the first digit. To solve that, I decided to use a <= operator but now it's giving the floating point exception (core dumped) error.

Found an answer! The reason was an overflow, since the condition `(n % i) <= n` would be true upto infinity and long long i would keep being multiplied by 100 until even long long cannot hold the value. Instead, I used the condition `i <= pow(10,length)`, also providing the length to the functions and now it works perfectly.