Floating Point Exception C Why and what is it? -
the mistake occurs, when change value of n in loop 2. pretty sure, not divide 0, though. need multiply every other digit in cardnumber 2 , add summ(though, in case if other digit * 2 two digit number, must make 2 single-digit numbers out of e.g 14 becomes 1 4 )
long cardnumber = 378282246310005; int luhn = 0; /*luhn's algorithm*/ for(int n = 2; n <16; n = n + 2) { /*check digits exceed 10*/ if(( ( ( cardnumber % (10^n) ) *2) > 10)) luhn += ( cardnumber % (10^n) ) + 1; else luhn += ( cardnumber % (10^n) ) * 2; } printf("%d", luhn);
^
operator bitwise xor, not exponentiation. when n=10 expression 10^n
evaluates 0 causes cardnumber % (10^n)
become divide zero.
Comments
Post a Comment