+ 3

Hey friends please explain me the code and say why its output is 9...

include <iostream> using namespace std; int main() { int sum =0; int count=1; while(sum<8){ sum+=count; count =count+2; } cout<<sum; return 0; } //out put is 9

27th Aug 2018, 5:16 AM
Albert Whittaker :Shaken To Code
Albert Whittaker :Shaken To Code - avatar
1 Odpowiedź
+ 2
in the while loop * the first loop sum = 0 + 1 = 1; count = 1 + 2 = 3; *the second loop sum = 1 + 3 = 4; count = 3 + 2 = 5; *the third loop sum = 4 + 5 = 9; count = 5 + 2 = 7; at this time the while loop stop because the value of sum is 9 so it is greater than 8. so the final value of sum is 9 and the final value of count is 7
27th Aug 2018, 5:27 AM
estifanos
estifanos - avatar