Explain why this code outputs this answer in detail C includ

Explain why this code outputs this answer in detail (C++) #include using namespace std; int main() {int sum = 0, i=0; while(i

Solution

#include <iostream>

using namespace std;

int main()
{
int sum=0,i=0;
while(i<=15)
{
sum=sum+i++;
cout<<\"Run \"<<i<<\" sum = \"<<sum<<endl;
if(i%5==0)
break;
i=i+1;

}
cout<<\"i = \"<<i<<\". \"<<endl;

return 0;
}

OUTPUT:
Run 1 sum = 0
Run 3 sum = 2
Run 5 sum = 6
i = 5.


Description :

step 1 :
       Intially sum=0, and i=0;
step 2:
       repeat the bock upto i<=15(while(i<=15))
       sum is added to i and initialised to sum, and i is increamented by 1(sum=sum+i++;)
       print the run i and sum
       if i is successfully divisible by 5 then break the loop while
       i is increament by 1
step 3:
       finally print the i
      

Iteration 1:
step 1: sum=0,i=0;
step 2:
       repeat loop i<=15 is true
       sum=0+0;
       sum=0; i=1;
step 3: prints \"Run 1 sum = 0\"
       checks i is divisible by 5(i%5==0) yiels false
       i is incrementd by 1--> i=2;
      
Iteration 2:
step 1: sum=0,i=2;
step 2:
       repeat loop i<=15 is true
       sum=0+2;
       sum=2; i=3;
step 3: prints \"Run 3 sum = 2\"
       checks i is divisible by 5(i%5==0) yiels false
       i is incrementd by 1--> i=4;

Iteration 3:
step 1: sum=2,i=4;
step 2:
       repeat loop i<=15 is true
       sum=2+4;
       sum=6; i=5;
step 3: prints \"Run 5 sum = 6\"
       checks i is divisible by 5(i%5==0) yiels true it breaks the loop
      
prints \"i = 5\"

 Explain why this code outputs this answer in detail (C++) #include using namespace std; int main() {int sum = 0, i=0; while(iSolution#include <iostream>
 Explain why this code outputs this answer in detail (C++) #include using namespace std; int main() {int sum = 0, i=0; while(iSolution#include <iostream>

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site