+ 3

Can anyone tell me working of this code.

This is code #include <iostream> using namespace std; int main() { int x=5; x=x*8%x; cout<<x; return 0; } it's output is 0. even when I changed 8 with any other number it is give output also 0.

21st Apr 2017, 8:08 AM
Rajesh Kumar (Rj)
Rajesh Kumar (Rj) - avatar
7 Respuestas
+ 8
Hmm... x = 5 x = 5*8 = 40 % 5 = 0 print x = 0 x = 8 x = 8*8 = 64 % 8 = 0 So far there is no error in the code and it gives the right output
21st Apr 2017, 8:12 AM
MrCoder
MrCoder - avatar
+ 6
@Rajesh Kumar(Rj) It would still output 0 (Refer to mine or M Rey's code)
21st Apr 2017, 8:16 AM
MrCoder
MrCoder - avatar
+ 5
a*x%x will give 0 for all integers a, as a*x always is divisible by x.
21st Apr 2017, 9:05 AM
Tob
Tob - avatar
+ 3
yeah, because 5*8=40 and 40%5=0; u can use for 8 any other number and it will be always returns 0.
21st Apr 2017, 8:12 AM
M Pax
M Pax - avatar
+ 3
if I change 8 with 5 .
21st Apr 2017, 8:15 AM
Rajesh Kumar (Rj)
Rajesh Kumar (Rj) - avatar
+ 1
test it, but i think it will returns 0
21st Apr 2017, 8:16 AM
M Pax
M Pax - avatar
+ 1
u should change ur x
21st Apr 2017, 8:16 AM
M Pax
M Pax - avatar