I am a beginner. I have a problem about % this can anyone explain me that how the output of print(4%2) is 0 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I am a beginner. I have a problem about % this can anyone explain me that how the output of print(4%2) is 0

28th Apr 2021, 1:16 PM
Rakesh Mondal
3 Answers
+ 2
% is totally different operator in programming from math . % is called modulus operator . it returns the remainder after division of 1st param by 2nd param . Here after dividing 4 by 2 we get remainder 0 . Another example 5%2 After dividing 5 by 2 we get quotient 2 in int and remainder 1 . So 5%2 = 1
28th Apr 2021, 1:20 PM
TOLUENE
TOLUENE - avatar
+ 2
% operator gives the remainder as output.. Here, 4%2 = 0 Coz, 4 is divisible by 2 and the remainder is 0 Lets take another example, 5%2 which results in 1. Coz, when you divide 5 by 2 the remainder is 1. It'll be easy if you know which part is called remainder when we do division in Mathematics. Just try to analyze it.. 2) 5 (2 4 _______ 1 //remainder part
28th Apr 2021, 1:22 PM
sarada lakshmi
sarada lakshmi - avatar
+ 1
Try deviding 4 with 2, it fits exactly 2 times into four, if however you devide 5 with 2, it only fits 2 times, since 2 × 2 = 4, and if you added another 2 it would surpass 5 and become 6, so, 5 % 2 will give you the distance from 4 and 5, if you did 5 - 4 you would get 1. When you do 4 % 2, 2 fits exactly 2 times into 4, so there is no distance, if you did 4 - 4 you would get 0.
28th Apr 2021, 1:23 PM
Karak10
Karak10 - avatar