Could anyone tell me the cout of this code? And please explain :) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Could anyone tell me the cout of this code? And please explain :)

int x = 0; x%=6; cout<<x;

20th Dec 2016, 4:50 AM
Desu~
Desu~ - avatar
7 Answers
+ 9
output is 0 because x%=6 which means x=x%6 then x will be assigned with a value after 0%6 expression is solved. 6*0=0 so the remainder after dividing 0 with 6 is 0.
20th Dec 2016, 4:58 AM
sai chaitanya raja
sai chaitanya raja - avatar
+ 7
0
21st Dec 2016, 8:32 AM
Klodian Lula
Klodian Lula - avatar
+ 2
About your second question : You can see that the loop will only execute 3 times ; for I = 1, 4, 7. Now consider the if statement for these 3 values: 10/1 = 10, so if evaluates to false. 10/4 = 2.5, but since the expression compares ints it is truncated to 2, so if evaluates to true. 10/7 = 1 for similar reason, so if evaluates to false. You should now be able to figure out the answer.
20th Dec 2016, 7:02 AM
Ettienne Gilbert
Ettienne Gilbert - avatar
+ 2
@Ettiene how about res? what's the use of it?
20th Dec 2016, 1:56 PM
Desu~
Desu~ - avatar
+ 1
i did try it everytime i encounter that question in challenges, i dont know why it didnt work anyways thank you.
20th Dec 2016, 5:00 AM
Desu~
Desu~ - avatar
+ 1
also i have this question dude, i dont how to solve this code int main(){ int res = 0 for (int i=1;i<10;i+=3) { if (10/i==2) continue; res+=i } cout<<res; }
20th Dec 2016, 5:02 AM
Desu~
Desu~ - avatar
+ 1
@Ranpo The value of i is added to res every time 'if' evaluates to false (since, if 'if' is true, then 'continue' is executed which jumps straight to the end of the loop - and then evaluates the next i for the next round) So, for 1st loop: i = 1; if is false, so we execute res+=i so res=1 For 2nd loop: i = 4; if is true, so res+=i is skipped; res still 1. For 3rd loop: i = 7; if is false, so we execute res+=i; res now 8. Answer: res is 8
20th Dec 2016, 5:51 PM
Ettienne Gilbert
Ettienne Gilbert - avatar