0

Can anyone help me understand this output?

Hi guys, I recently completed a challenge in C++ but I don't understand an output. The code give the variables i=2 and j=5 and ask to print the output after a modulus operation between them. Why the result is 2? I can't understand. For better understanding this is the entire block of code: int i=2,j=5,a=0,b; a=i%j; cout<<a; b=a%i; cout<<b; Output 20

9th May 2019, 1:19 PM
Stefano Di Somma
Stefano Di Somma - avatar
4 Antworten
+ 1
I found a better answer on the internet. To calculate the remainder we can use the formula r = a - (b*q) where a is the dividend, b is the divisor and q is the quotient, so r = 2 - (5*0).
10th May 2019, 1:06 PM
Stefano Di Somma
Stefano Di Somma - avatar
+ 2
Stefano Di Somma 5 cannot go in to 2, then the output is 2 because if you divide 2/5, you get 0 remainder 2.<-
9th May 2019, 2:13 PM
Pikachu
Pikachu - avatar
0
So the 2 you got is the value of i?
9th May 2019, 2:41 PM
Stefano Di Somma
Stefano Di Somma - avatar
0
2 modulo 5 is 2 since 2 can go into 5 2 times, so a = 2. The 2 from a modulo 2 gives you 0 since there is no remainder, thus b = 0. No endl, so your prints will be on one line.
9th May 2019, 4:47 PM
Choe
Choe - avatar