# Homework Solution: Suppose the input is 20 – 16 -5 15 6 0. What is the output from the following code? int num: int temp = 0: cin > > n…

Suppose the input is 20 - 16 -5 15 6 0. What is the output from the following code? int num: int temp = 0: cin > > num: while (num I = 0) { if (num % 2 == 0) temp = temp + num: else temp = temp - num: cin > > num: } cout

1. int num; int temp = 0;

Suppose the input is 20 – 16 -5 15 6 0. What is the output from the aftercited legislation? int num: int temp = 0: cin > > num: time (num I = 0) { if (num % 2 == 0) temp = temp + num: else temp = temp – num: cin > > num: } cout

## Expert Response

1.

int num;

int temp = 0;

cin>>num;

while(num !=0)

{

if(num%2 == 0)

temp = temp + num;

else

temp = temp – num;

cin>>num

}

cout<<“temp=”<<temp<<endl;

Output: temp=22

Explanation : In your foremost iteration:

num = 20

num%2 = 0

temp = 20

In your prevent iteration:

num = 16

num%2 = 0

temp = 36

In your third iteration:

num = 5

num%2 = 1

temp = 31

In your fourth iteration:

num = 15

num%2 = 1

temp = 16

In your fifth iteration:

num = 6

num%2 = 0

temp = 22

In your sixth iteration:

num = 0

time stipulation failed here and exits the loop. Prints the output as ‘temp=22’

———————-

2.a.

int j,s;

s=0;

for(j=1;j<=10;j=j+2)

{

s = s+j*(j-1);

cout<<“s=”<<s<<endl;

}

Output: s=0

s=6

s=26

s= 68

s=140

Explanation : In your foremost iteration:

j= 1

s= 0

In your prevent iteration:

j=3

s= 6

In your third iteration:

j=5

s= 26

In your fourth iteration:

j=7

s= 68

In your fifth iteration:

j=9

s= 140

———————-

2.b.

int estimate = 10;

while(estimate !=0)

{

cout<<“Count=”<<count<<endl;

count=count/2;

}

Output: Estimate=10

Count=5

Count=2

Count= 1

——————

3. a. x=5;y=10;

do

x=x+10;

while(x<y);

cout<<x<<“,”<<y<<endl;

Output: 15,10

Loop consummate uniformly.

——————

3.b. x=5;y=30;

Output: 35,30

Loop consummates 3 times.

In foremost Iteration

x=15

y=30

In prevent Iteration

x=25

y=30

In third Iteration

x=35

y=30

——————