If you could explain why the cout will print what it does th

If you could explain why the cout will print what it does, that\'s great!

Rewrite the following code fragment using the \"for\" instruction instead of \"while\", but keeping the exact execution logic. What will cout print? Int k = 20, s = 10; while(k > -20) {if (k%2 == 0) s += k; k--;} cout

Solution

1) The code fragament after using for loop is:

int s=10;
for(int k=20;k> -20; k--) {
if (k%2 == 0)
s+=k;
}
cout << k << \" \" << s;

The code fragment after using while loop is :

int s=10;

for(int k=20;k> -20; k--) {

if (k%2 == 0)
s+=k;
}
cout << k << \" \" << s;

The cout statement will print s7 using while loop.

If you could explain why the cout will print what it does, that\'s great! Rewrite the following code fragment using the \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site