math - Issue with % operator in C -
i having issue % operator in c. know % operator gives remainder of division. when faced question 1 % 2
or 3 % 2
, confused. after googling this, found different solutions.
some 1 / 2
0.5
, round down 0. 1 % 2
0.
others 1 / 2
0.5
, instead round up, in maths, 1. 1 % 2
1.
and therefore, confused. question is: what 1 % 2?
thank in advance :):)
simply put, both wrong methods. said %
finds remainder of division.
therefore 1/2 equal 0 remainder 1.
and answer 1.
also, experiment yourself, have used program:
#include <stdio.h> main() { int remainder; remainder = 1 % 2; printf("1 %% 2 %d", remainder); return(0); }
hope helps :)
Comments
Post a Comment