Why is 1%2 = 1? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 15

Why is 1%2 = 1?

I just saw that print(1%2) outputs 1 in Python and it is similar in other languages like C or C++. I am studying programming for quite some time and never gave it a thought. Does anyone know why is that?

28th May 2019, 1:15 PM
Ali Anwar
Ali Anwar - avatar
20 Answers
+ 40
Because if you don't have enough cookies to give each kid at least one, the quotient is 0 (each kid gets 0 cookies) and the remainder is the same as the number of cookies you have (as the kids don't get any cookies). 4 cookies, 5 children => each child gets 0 cookies and 4 will be left over. Sorry if the cookie example is a bit infantile, but it's the best way to understand it IMO 😅
28th May 2019, 1:50 PM
Anna
Anna - avatar
+ 20
An equation with this symbol ( % )the division modulo yields only the remainder portion of the calculation.
28th May 2019, 9:17 PM
ron
+ 14
Well bro it's that because % this operator is known as modulo and it returns the remainder. Now 1 divided by 2 gives a remainder of 1 and quotient 0 . As 2 = 1*0 +1 Hence 1 % 2=1 Thanks
28th May 2019, 1:18 PM
Prince PS[Not_Active]
Prince PS[Not_Active] - avatar
+ 9
Because the remainder is 1.
29th May 2019, 9:12 AM
Sonic
Sonic - avatar
+ 8
Ali Anwar Floating point modulo: You have 23.7 cookies and 7.3 children. How many cookies does each child get? => 23.7 // 7.3 = 3 How many cookies do you need to give every kid 3 cookies? => 3 cookies * 7.3 kids = 21.9 cookies How many cookies will be left? => 23.7 - 21.9 = 1.8 ==> 23.7 % 7.3 = 1.8
28th May 2019, 1:38 PM
Anna
Anna - avatar
+ 7
Gonza G. The question is not about SQL
29th May 2019, 9:19 AM
Anna
Anna - avatar
+ 6
modulo is used to return the remainder if any while division is used to know how many times a number can go in another number example 9/3=3 9%3=0 because % returns the remainder hope it helped
29th May 2019, 11:47 AM
Dace [Super Inactive| Challenge=Maybe]
Dace [Super Inactive| Challenge=Maybe] - avatar
+ 4
Thanks Prince PS for making it clear. I also noticed that Python allows to use a modulo like this "1.3%2", however "C" doesn't allow it. Floating point modulo doesn't make sense to me.
28th May 2019, 1:29 PM
Ali Anwar
Ali Anwar - avatar
+ 3
Hola! Espero poder ayudarte, solo hablo español, el operador "%" sirve para sacar la parte entera de una división o también conocido como módulo de la división. En el caso de usar "%" en SQL sirve para hacer una consulta usando algún comodín. Eso es lo que entendí. Saludos
29th May 2019, 8:32 AM
Gonza G.
Gonza G. - avatar
+ 2
I ask you a question : What is the remainder of 1 / 2 in an Euclidean division? //that will be 1
28th May 2019, 1:18 PM
Théophile
Théophile - avatar
+ 2
By the way your equation doesn't make sense Prince PS because 2 != 1*0 + 1
28th May 2019, 1:32 PM
Ali Anwar
Ali Anwar - avatar
+ 2
Anna Thanks. But what if "1.2%2" where 1.2 < 2. In this case the output is 1.2, why is that?
28th May 2019, 1:46 PM
Ali Anwar
Ali Anwar - avatar
+ 2
The “%” operator gives you the remainder of the division if the divisor (e.g. 2) is bigger than the dividend (e.g. 1.2) means that the dividend contains 0 (zero) times the divisor and the remainder is equal to the dividend. Hope this helps BTW you can see it as 1=2*0+1
29th May 2019, 7:25 AM
mecmarco
mecmarco - avatar
+ 2
D=d*q+r 1=2*0+1 If we divide 1 by 2 it will be divided by 0 and we will get 1 as a remainder Remember->whenever denominator is greater than numerator in % case.just write the numerator as a answer..
29th May 2019, 7:41 PM
Ajay
Ajay - avatar
+ 2
It's simple bro. As you already know that % operator gives remainder. Then we can write 1%2 as--- 2 ) 1 ( 0 0 ----- 1 ----- So by just simple division we got the remainder 1. Hope you understand it easily by this method.
30th May 2019, 1:31 AM
Laksheya
Laksheya - avatar
+ 1
% represents remainder so if you divide 1 by 2 obviously the remainder will be 1 thats why the ans is 1.
29th May 2019, 3:15 PM
Juveria Welder
Juveria Welder - avatar
+ 1
if small number module by large number it's remainder is always smallest number
29th May 2019, 3:47 PM
swathi sri
0
The remainder of 1/2
29th May 2019, 8:29 PM
Roj Serbest
0
Modulo is mathematical thing it’s what we call remainder or the left over of division.
29th May 2019, 10:39 PM
Soufiane
Soufiane - avatar
- 3
Learn math
29th May 2019, 8:07 AM
Najmuddin Ansari
Najmuddin Ansari - avatar