+ 1

modulo explaination

int value = 23; int res = value % 6; //res is 5 the anwser is there i just dont understand how it got there. please explain why it got there with work shown please. thanks.

17th Aug 2016, 1:26 AM
Michael Essex
Michael Essex - avatar
3 Réponses
+ 12
Modulus is the remainder of a division. 6 goes into 23 three times evenly = 18. Remainder of 23 and 18 is 5. If value was 24, modulus would be 0 because 6 goes into 24 four times evenly, no remainder.
17th Aug 2016, 2:20 AM
James
James - avatar
+ 4
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 23 apples and you want to pack them in groups of 6. 23%6 will give you the number of apples left after all the groupings are done. In this case, you will get 3 groups of 6 apples accounting for 18 apples with 5 apples left. These 5 apples cannot be packed into groups of 6 as 5<6 So 23 = 6*3 + 5 This means 23%6 = 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:25 PM
Dhaval Furia
Dhaval Furia - avatar
0
well just do the simple division n the remainder will be your answer. kudos you know the explanation of modulus
10th Nov 2016, 5:24 PM
Senpai Anubhav
Senpai Anubhav - avatar