If 2 goes into 5 two times, then 7%(5//2) = 7%2 which would be 3.5, but 3.5 isn't an answer. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

If 2 goes into 5 two times, then 7%(5//2) = 7%2 which would be 3.5, but 3.5 isn't an answer.

I need an explanation...

10th May 2019, 6:36 AM
t@y|0r
4 Answers
+ 1
X%Y = X - (X//Y)*Y 7%2 = 7 - (7//2)*2 = 7 - 3*2 = 1
10th May 2019, 9:18 AM
Seb TheS
Seb TheS - avatar
10th May 2019, 6:57 AM
Daniel Adam
Daniel Adam - avatar
0
% isn't the division operator, it gets the *remainder* of a division 2 goes into 7 three times, and then gets the remainder: which is 1 Thus, 7 % 2 returns a value of 1
10th May 2019, 7:18 AM
Trigger
Trigger - avatar
0
The % is modulo; it deals mainly with dividing whole numbers, and gives you the remainder when it reaches it's limit. EG: 7%2 So first we see how many times 2 will fit in 7 as a whole number: 2 x 1 = 2 - still lower than 7 2 x 2 = 4 - still lover than 7 2 x 3 = 6 - still lower than 7 2 x 4 = 8 - higher than 7 Now to complete the modulo, we do 7 - 6 which equals 1, your result! Your program does these calculations for you, all you need to understand is you are seeing what's left over, not the result of a division.
10th May 2019, 7:23 AM
Rincewind
Rincewind - avatar