0
var a=66; var b=a % 4; var c=b % a; document.write(c); ans is 2 how ??? i thought ans is zero coz b value is 2 and go to nxt step c=b%a 2%66 there is no reminder it will divide completely without reminder so ans is zero plz explain me clearly guyz 😇😶
5 odpowiedzi
+ 7
2 divided by 66 is 0, remainder 2. So the output is 2.
+ 4
You were right up until your last statement. The remainder of 2/66 is not zero. This is because division only counts whole quantities. What is 4/2? 2, because you can split 4 into 2 units of 2. What is 4%2? 0, because after the 2 units of 2 are made, there is nothing left. However, what is 2/4? We cannot do anything with that. Not even one unit of 4 can be made out of the 2, as two is less than 4. Thus, we have a 2 that we really cannot do anything with, and the remainder is 2. Thus, 2%4 is 2. In the same way, we cannot even make one unit of 66 out of this tiny 2. Thus, the 2 is left over, and is considered the remainder. THUS, 2%66 is 2, as no division can be performed.
+ 4
Principle of modulo ( % ), rest of integer division, mathematicaly is:
for A, B, R, n integers,
A/B = n*B + R
R is the rest of integer division of A by B, and is called modulo, so:
A%B = R
In particular case where A < B,
then A/B = 0*B + A...
result of integer division is 0, rest is A,
so A%B = A...
This was to be demonstrated ^^
+ 1
var a = 66;
var b = a % 4; // 16 * 4 = 64 + 2
var c = b % a; // 2 / 66 => 2
document.write(c);
if second parametr is bigger then first it return first parametr.
- 1
replace "var c=b % a" with "var c=a % b" and now re run the code, u will get your answer.
Since b=2 and a=66
66%2 = 0
but 2%66 = 2