c++cmodulo

0%2 coming out to be 1


I am having a weird problem. Here is the link to the code I am working on with input and output. The snippet on which I am having problem is posted here

int r, ans;
long long b;

printf("before modulo r= %d ,ans =%d\n",r/b,ans );
ans=(ans)%2;
printf("after modulo r = %d ,ans =%d\n",r,ans);

One can see that in the second iteration before executing line 24 ans = 0,after that it becomes 1 instead of remaining 0 as 0%2=0 . I really cannot understand the problem


Solution

  • Your first print doesn't print ans, as your b is a long long int, so r/b is a long long int, which has 64 bits, and your first print will print the first 32 bit as r and the next 32 bit as ans. Your ans is never 0 to begin with.

    Please, next time, post your code instead of linking it.