I need help understanding the modulus operator (%). | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 11

I need help understanding the modulus operator (%).

I'm trying to learn C# and not knowing how this operator works is making it really hard for me to learn C#. I tried to go back to basic concepts to figure it out and looked at the commits explaining how it works but I just can't seem to understand it and that really bothers me. Can anyone PLZ help me understand how the modulus operator works, I'm kinda a slow learner.

22nd May 2017, 9:57 PM
Hyper jenkins
Hyper jenkins - avatar
54 Answers
+ 13
Nvm it wasn't because of the float. You probably got 0 because the remainder was 0. e.g: 4%2 = 0 Test this and play around with it: https://code.sololearn.com/caPnERkLp9oo/?ref=app
22nd May 2017, 10:31 PM
Tim G
Tim G - avatar
+ 18
Modulus get's the remainder of a divison. e.g: The remainder of 7/3 would be 1 because the "3" fits two times into the 7. So the remainder is 7-(3*2) Modulus is the same as this: int a = 7; int b = 3; int modulo = a - ((a/b)*b);
22nd May 2017, 10:02 PM
Tim G
Tim G - avatar
+ 13
Np, you're welcome! Don't bother to ask questions, because that's the way we learn!😉 Considering an easy example: we have two integers: a(5) and b(2). When we try to divide 5 by 2, then we get an integer that tells us how many times the 2 fits in the 5. In this example 5/2 is 2; Now we know that "2" fits at least two times into the 5. So we look at what we get when we multiply them. Means: 2*2 = 4. Now we have the 4, and the last thing that we have to do to find the remainder, is to find the difference between a(5) and the 4. Means: 5-4 = 1(That's the remainder) And that's it :D. Short form is a=5 b=2 5-((5/2)*2) 5-((2)*2) 5-(4) 5-4 = 1 I hope I could help you.
22nd May 2017, 10:53 PM
Tim G
Tim G - avatar
+ 11
Look at the Q&A, this question had been asked MAANNYY times. It is a great question but there are already many answers out there 😉
24th May 2017, 12:33 AM
C.E.
C.E. - avatar
+ 7
Nice! Haha np(:
22nd May 2017, 11:07 PM
Tim G
Tim G - avatar
+ 7
just the remainder after a divide operation 5%2 : remainder =1. ... 2*2=4 +1 3%5 : remainder =3. ... 5>3 so 3 6%3: remainder =0.... 3*2=6 exact multiple ao 0
30th May 2017, 3:23 AM
manoj katari
manoj katari - avatar
+ 6
When the value before the modulos operator is smaller than the value after it, then it shows the first one. Because it doesn't fit in once, means the remainder is the full number
22nd May 2017, 10:14 PM
Tim G
Tim G - avatar
+ 5
If you are stuck in Math, close your eyes doe a while ans think of a base case that works EXACTLY like your problem, then apply the algorithm to your big problem. E.g. How many numners from 1 to 100 inclusive contain the digit 1, can have a base case of how many 1s are there from 1 to 10, 11 to 20, 21 to 30.
22nd May 2017, 11:30 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 5
5 % 3 = 2 the remainder of 5 divided by 3 is 2
23rd May 2017, 10:17 PM
NimWing Yuan
NimWing Yuan - avatar
+ 5
Modulo returns the remainder. say we have 7%3, lets do it in a layman's method: so first step is 7/3[7 divide by 3], question will be 2 (3x2= 6), 7-6=1, here 1 is remainder, so here 1 is result. 7%3=1.
24th May 2017, 3:51 AM
Popat
Popat - avatar
+ 4
if I understand your question correctly, maybe 13
22nd May 2017, 11:46 PM
Hyper jenkins
Hyper jenkins - avatar
+ 4
By using modulas(%) you can find remainder of a number when dividing by another number.. for example.. 11%5 ur answer will b 1.. You can also find a number wheter its even or odd by applying some condition like if(no%2==0)then its even number (since even number completly divisible by 2 so there will be no remainder) else its odd..
25th May 2017, 3:11 AM
Mrinmoy Kalita
Mrinmoy Kalita - avatar
+ 3
I'm saving this to my memo so I can always reference it. this is super helpful. Thanks TONS!
22nd May 2017, 10:58 PM
Hyper jenkins
Hyper jenkins - avatar
+ 3
3\2=1 3%2=1 2*1+1=3
23rd May 2017, 9:10 PM
Mohammad Hossein Zangooe
Mohammad Hossein Zangooe - avatar
+ 3
the %operator gives the remainder of a division operation example 3%2 gives 1 or 2%2 gives 0
24th May 2017, 5:51 AM
James Njoroge
James Njoroge - avatar
+ 3
basically the module work is to get reminder in the operation of division just like public static void main(string args[]){ int a=5%2; System.out.println(a); // the answer is 1 w because when we divide 5 in two whole number each will get 2 but remaining number is 1
24th May 2017, 1:27 PM
Abdulwahhab Khan
Abdulwahhab Khan - avatar
+ 3
so basically...u need to k ow about divide( symbol = / ) or ( usually in math = ÷ ) example : 12 / 4 = 3 in modulus, usually they make like this one : example : 13 ℅ 4 = ? first,just think '%' is '/' 13 can't divided with 4,the closest number is 12, cuz like I wrote first, 12/4 = 3. so we just need to use subtraction (symbol = - ),or usually we say 'min'. so, 13 - 12 = 1. then, the answer is 1 (13 % 4 = 1). another examples : • 27 % 5 = 2 ~ ( 25 / 5 = 5 , so 27 - 25 = 2). • 9 % 2 = 1 • 44 % 6 = 2 u can take it slow..its okay😄,I'm still beginner too and maybe its crazy I just knowed it few minutes ago and now I try to give my opinion in this question😂,but well... good luck friends😊
25th May 2017, 6:31 AM
FLASH!!!
FLASH!!! - avatar
+ 2
That's how I thought It might work but when I was looking around trying to understand the modulus operator I found this: 5 % 6 = 5 and 2 % 5 = 2
22nd May 2017, 10:09 PM
Hyper jenkins
Hyper jenkins - avatar
+ 2
I didn't know that, how come, I don't understand why?
22nd May 2017, 10:18 PM
Hyper jenkins
Hyper jenkins - avatar
+ 2
Thanks very much, I think I got it now!!!
22nd May 2017, 10:41 PM
Hyper jenkins
Hyper jenkins - avatar