I want to divide 5 by 3 using C#. What operator can I use to get the remainder or modulus after dividing?
Asked
Active
Viewed 1.2e+01k times
23
-
Did you try something for this ? to start of check `/` and `%` and their usage, good if you can come up with a specific issue with the code attempting this. – V4Vendetta Feb 13 '13 at 07:16
-
11`int remainder, quotient = Math.DivRem(5, 3, out remainder);` – Slai May 04 '16 at 17:20
-
7This page comes up third in search results for 'c# remainder' after MSDN and has been viewed 23988 times so this is a very useful question to many. Additionally the question http://stackoverflow.com/questions/5383050/how-can-i-calculate-divide-and-modulo-for-integers asks a very similar question with similar answers and has not been closed.In fact it has been highly upvoted. Either both should be closed or both opened. I don't have enough points to vote to re-open this, but if I did I would. – usefulcat Jun 08 '16 at 10:14
-
8It's always amusing when I google something and one of the first results is a page where people are telling someone to google it. Thanks to those who gave an answer. – ingredient_15939 Mar 22 '18 at 05:05
-
1Agreed, this is very much a Q & A question, I don't see why it was closed for the stated reason. Voting to reopen. – Douglas Gaskell Jan 02 '20 at 01:50
1 Answers
64
You can do this:
double answer = 5.0/3.0;
int remainder = 5 % 3;
int quotient = 5 / 3;

T.Z
- 964
- 2
- 9
- 15