 Why is 1%2 = 1? | Sololearn: Learn to code for FREE!

+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?

+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 😅

+20

An equation with this symbol ( % )the division modulo yields only the remainder portion of the calculation.

+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

+9

Because the remainder is 1.

+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

+7

Gonza G. The question is not about SQL

+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

+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.

+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

+2

I ask you a question : What is the remainder of 1 / 2 in an Euclidean division? //that will be 1

+2

By the way your equation doesn't make sense Prince PS because 2 != 1*0 + 1

+2

Anna Thanks. But what if "1.2%2" where 1.2 < 2. In this case the output is 1.2, why is that?

+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

+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..

+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.

+2

Because in c when numerator is less than denominator..at that time the answer of modulus is itself the numerator

+1

% represents remainder so if you divide 1 by 2 obviously the remainder will be 1 thats why the ans is 1.

+1

if small number module by large number it's remainder is always smallest number

0

0

Modulo is mathematical thing it’s what we call remainder or the left over of division.