Problem using the Modulo

Why 5 % 2 is equals to 1? How can I get the quotient of 1 manually?

1
2
3
4
5
6
7
  #include <iostream>
using namespace std;

int main(){
	
	cout<<5 % 2<<endl;
}
5 % 2 gives you the remainder of the division 5 / 2.

          this is the integer quotient (C++: 5 / 2)
          ↓
5 / 2  =  2 + 1 / 2
              
              this is the remainder (C++: 5 % 2)
Last edited on
Peter87 where did you get that 2 before number 1? And did you just copy the denominator from the left to right?
Last edited on
where did you get that 2 before number 1?

It's the quotient of the division that has been rounded down to the closest integer.

did you just copy the denominator from the left to right?

Yes.


If you search for integer division you'll find lots of information that explains how it works.
Last edited on
Thanks Peter87! I have now get it. I just divided 5 by 2 manually then getting the remainder of 1. What I did wrong before I got the answer is that:

(Answered manually.)

5 / 2 = 2
2 x 2 = 4
5 - 4 = 1 (this is where you get the answer which is the REMAINDER)
But my little brain continued to answer and I had added decimal point after 5 then adding 0 which is WRONG! haha...
10 / 2 = 5
5 x 2 = 10
10 - 10 = 0
Now I got the total answer of 2.5 and I thought that 5 is the answer of 5 % 2... lol.
Topic archived. No new replies allowed.