Question

I have an issue here. I have to make a program that checks if a credit card number is valid or not using the checksum. I'm supposed to multiply every other digit starting from the second to last digit, then add the products then add the numbers that weren't multiplied to that sum as well. The result should produce 0 when divided. However, Im having an issue here with my program. When I enter large numbers, the values change up and at the end I get Floating point exception(core dumped). When I enter smaller numbers, sometimes it works, sometimes it doesn't. Please help me out.

Thank you for your help. Please explain the issue so I can avoid it later.

Was it helpful?

Solution

I think you may have a problem when iterating i up to a large cardNum because cardNum is long long which can hold huge numbers but int i is just an int relatively small.

To solve this problem try holding the cardNum as a string extracting each digit from it and parsing them into an int. You can then multiply and add them up without dealing with representing huge numbers.

OTHER TIPS

cardnum needs to be a string not a number, you are looping over it expecting to get each digit. YOur for loop will give you all the number from 1 to the credit card number (a long loop)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top