Hi guys! Can you tell me please how the operator % work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Hi guys! Can you tell me please how the operator % work?

Logical operator

22nd Nov 2021, 8:26 PM
Samuele Mondello
2 Answers
+ 1
It count the rest of the division, we are operating on natural numbers so the result must be >=0 for example 5 % 2 = 1 because: 5 - 2 = 3 rest: 0 3 - 2 = 1 rest: 0 1 - 2 = 0 rest: 1 because 2 is greater than 1
22nd Nov 2021, 8:51 PM
Bartłomiej Dyło
0
As is correctly stated by Dylo, the %-Operator gives you the remainder upon division. Let me illustrate how that works. Take any integer m, say 12. Take any other integer n, say 5. To find the value of 12%5, you first ask yourself: How often can I put 5 into 12? Well, not more than 2 times. Why? Because 2*5 = 10, so that is fine; but 3*5 = 15, which is larger than 12. Now, given that, you ask the next question: After putting all the 5's in, how much is left? For that you compute 12 - (2*5) = 12 - 10 = 2. And this 2 is the result of 12%5. The technical term, I guess especially in programming, is modulo operation. The process by which one obtains the result (even for huge numbers) is called Euclidean division. I am naming these things just in case you want to look them up ;P.
7th Jan 2022, 8:48 AM
Giuseppe T
Giuseppe T - avatar