How does modulo operate cant get the concept | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How does modulo operate cant get the concept

i need to understand how the numbers are manipulated like in subtraction or addition

7th Nov 2016, 9:57 AM
masterchief
4 Answers
+ 2
In simple words, a%b gives the remainder when a is divided by b Most students are taught that "Dividend = Divisor * Quotient + Remainder" In more sophisticated language, this is the Division Algorithm : For any integers a and b such that b is non-zero, there exist unique integers q and r such that a = b*q + r where 0<=r<b So a%b gives r For example, 9 = 2*4 + 1 So 9%2 = 1 Another way to think about it is something like this. Imagine you have 26 apples and you want to pack them in groups of 7. 26%7 will give you the number of apples left after all the groupings are done. In this case, you will get 3 groups of 7 apples accounting for 21 apples with 5 apples left. These 5 apples cannot be packed into groups of 7 as 5<7 So 26 = 7*3 + 5 This means 26%7 = 5 Note that making 0 groups is allowed, but the number of remaining apples should be less than that in each group.
19th Jan 2017, 12:26 PM
Dhaval Furia
Dhaval Furia - avatar
+ 1
modulo basically return odd or even
7th Nov 2016, 10:56 AM
Wanderlei Borges
Wanderlei Borges - avatar
+ 1
The modulo operator gives you the remainder from a devision. For example "31 / 10" would be "3", because "31" and "10" are int's. The remainder from this devision would be therefore "1". To get this remainder you write "31 % 10"
7th Nov 2016, 10:56 AM
Danny Hale
Danny Hale - avatar
+ 1
To clear out your second part of a question, where you ask how the numbers are manipulated in subtraction or addition, you have to keep an eye on the datatype of your numbers, respectively the type where you store your result. If you try to subtract or add floating point numbers and store them in a int container the value will be cropped to fit the container properties. Int container will hold just whole numbers, while for example double container hold floating point numbers.
7th Nov 2016, 11:09 AM
Danny Hale
Danny Hale - avatar