For each of the following give the number of times that the

For each of the following give the number of times that the statement x = x + 1.0 is executed. Give the value of num upon exit from the following

Solution

The following answers are made under the essumption that \"Count\" and \"count\" in the program are representing the same variable.

Question 2 a)

Ans : 99

Reason: initially num = 0 and Count = 1; in the next iteration num = 1 and Count = 2; So in the last iteration num = 99 and Count = 100; the while loop is not true extit the loop with num = 99

Question 2 b)

Ans : 5

Reason: Initially num = 0 and count = 2; In the next iteration num = 1 and count = 4; In this fassion when count = 10 num = 4; So in the lost loop num = 5 and count = 12; thus the loop exit with num =5.

Question 2 c)

Ans : 6

Reason: Initially num =0 and count = 2; In the first loop first iteration num = 1 and count = 4 similar to the previous problem hence the first loop will exit with num =5 and count =12. The next loop num =6 and count =13 but the while expresstion is false hence the second loop will exit with num =6 and count =13.

Question 2 d)

Ans : 10

Reason: Initially num=0; In the first iteration I=1 and hance num =1, upto I=10 the for loop condition will be true hence num=10 after that I will be 11 and loop exits with num=10

Question 2 e)

Ans : 1

Reason:   Initially num=0; In the first iteration I=10 and hance num =10, upto I=1 the for loop condition will be true hence num=1 after that I will be 0 and loop exits with num=1

Question 2 f) [ The question is valid if num have value before the loop, hence the following answer is given under the assumption that num = 0 initially insted of a = 0 ]

Ans : 55

Reason: Initially num =0; In the first iteration num will be updated with the sum of a and num hance num = 1; in the next iteration num = 1+2 = 3, similarly it adds the integers up to 10; thus at the end of the loop num=1+2+3+4+...+9+10 = 55

Question 2 g)

Ans : 30

Reason: Initally num = 0, In the first iteration of the first loop b = 1 and the condition is true so the second loop will run its first iteration and c = 1 and as in the previous question it will add the numbers 1,2,3 ext up to 5 and hence second loop exits with num = 0+1+2+3+4+5 = 15. in the second iteration of first loop b = 2 and the condition is true So the second loop will work again hence num = 15+1+2+3+4+5 = 30 .

Question 2 h)

Ans : 1

Reason: Initially num = 0 and count =2; the do while loop will exicut one without any condition checking hance num = 1 and count =4. The loop will not exicute any more because the condition faild and exit with num = 1

Question 2 i)

Ans : 32

Reason: Initially num = 0 the first loop will add 1 to num that is num = 0+1=1; The second loop will then add num = 1+1+2+3+4+5 = 16; again the first loop will add 2 to num that is num = 16+2=17; again the second loop will add 15 to num that is num = 17+1+2+3+4+5 = 32

 For each of the following give the number of times that the statement x = x + 1.0 is executed. Give the value of num upon exit from the following SolutionThe f
 For each of the following give the number of times that the statement x = x + 1.0 is executed. Give the value of num upon exit from the following SolutionThe f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site