Reputation: 1144
I'm just starting to learn C and tried to create a code to find 5 digits number that if it is multiplied by 4 will create a reverse of the 5 digits number. This is my code:
#include <stdio.h>
int main(void){
int i, result, modulo, div1, div2;
modulo = 10;
for (i = 12345; i < 99999; i++){
result = i*4;
div1 = 10000;
div2 = 1;
while ((i/div1)%modulo == (result/div2)%modulo){
div1 /= 10;
div2 *= 10;
if (div2 == 100000){
printf("%d", i);
}
}
}
}
and I got
floating point exception
I believe the problem is in the while statement condition, but I don't know what is the cause is. Anybody can explain my what's wrong? Thanks a lot.
Upvotes: 0
Views: 174
Reputation: 537
The reason is division by 0 in the line where you have div1 /= 10. You see, div1 is an integer, so each time you divide it by 10, it gets smaller, and after many iterations, couple of thousand of them, it gets 0. Division by 0 causes the problem. Put some if around the code to check if it is 0, and see what will you do if it is.
Upvotes: 2