When negative number is divided by its positive counterpart will give remainder 0;
for eg. cout<<(-5)%(5); //will print 0
BUT, a problem is that it may return negative remainders also, which is mathematically wrong
for eg. cout<(-5)%2; //will print -1
//actually it should have given remainder 1 not -1
NOTE....
C++ with its math.h (or cmath) provides a function called "remainder(x,y)", it corrects this problem as well as it is more advance since it can also give floating point remainders... You may use that IF you think such case/need can be encountered
remainder() requires two arguments () of any arithmetic type
for eg. cout<<(-5)%(5); //will print 0
BUT, a problem is that it may return negative remainders also, which is mathematically wrong
for eg. cout<(-5)%2; //will print -1
//actually it should have given remainder 1 not -1
NOTE....
C++ with its math.h (or cmath) provides a function called "remainder(x,y)", it corrects this problem as well as it is more advance since it can also give floating point remainders... You may use that IF you think such case/need can be encountered
remainder() requires two arguments () of any arithmetic type
Comments
Post a Comment
You are free to leave any kind of suggestion, or improvement. It doesn't require you to sign in either.