outpu does the following program fragment produce? 1, What while (i 64) printf d
ID: 3851160 • Letter: O
Question
outpu does the following program fragment produce? 1, What while (i 64) printf d i); What output does the following program fragment produce? i sa 1234 do print f sd i); i 10; while li 0) 3 show the output of each of the code blocks below: a) for (i 13; i 3 i 2) printf sd n", i); b) for (i i 0: i 8) printf "sd n", i) 4. Write C statements to sum the even integers from 1 and 100 using a for statement. Assume the integer variables sum and count have been defined, but not initialized, to be of type int. te for statements that print the following sequences of values: 5 a) 5, 10, 15, 20, 25 by 100, 90, 80, 70, 60, 50, 40, 30, 20, 10 Page 179 Focus on FundamantulExplanation / Answer
Answers:
(1). 1 2 4 8 16 32 64
Description: Here, the integer i is initialized with 1. It is given in while loop and till its value is less or equal to 64, the value gets printed by separating with a space. So, for the first run, it will print 1. Then it is multiplied by 2 every time and the value is printed till less than or equal to 64. So, next time 2, then 4, .... are printed till 64.
(2). 1234 123 12 1
Description: Here, do while loop is used so at least one time loop is going to be executed for sure. For the first iteration, the loop prints value of i, which is 1234. Now, the value of i is divided by 10 and assigned back to i itself. So, division is 123.4 but as i is an integer, value after decimal place is ignored so it becomes 123. So, 1 digit from the right side is removed right. So, this goes on till it reaches 1 and till time it has printed 1234 then 123 then 12 then 1. So as they are greater than 0, they are printed separating by a space. Now, 1 / 10 = 0.1 so in integer it becomes 0 and so the loop is terminated afterwards. So, the output is as above.
(3).
(a)
13
15
17
19
21
Description: Here, i is initialized with 13 and it is printed till its value is less than 23 and i is added with 2 after every iteration of the loop so it skips the next value and prints second text. Like it printed 13 then 13+2=15 so print 15 then 17 then 19 then 21, on new lines respectively. Now 21 + 2 = 23 so the loop condition becomes false as i<23 is defined. So, the loop exits then,
(b)
30
22
14
6
Description: Here, starting from 30, the values are printed on separate line and decrement by 8 till it is greater than 0.
(4). variables sum and count are defined so you can directly write it as below:
sum = 0;
for(count = 2; count <=100; count+=2)
{
sum+=count;
}
Description: To do sum in correct way, we need to initialize sum with 0 so that from the first loop itself it does like 0+2 then 2 + 4 then 6 + 6 then 12 + 8 and so on.... So, we do not require to find whether the number is even or not if I am initializing from 2 and adding 2 every time. This is the efficient code. So, after this loop, if we print the value of sum, it prints 2550.
(5). Assuming i is an integer variable already defined, the following loops give the answers.
(a) for(i=5;i<=25;i+=5)
{
printf("%d ",i);
}
(b). for(i=100;i>0;i-=10)
{
printf("%d ",i);
}
(6). The given code is going in an infinite loop because it is looping on the value of num variable and at first, it is 50, so the sum is added 50. Now, to add 49, the statement num-- should be written in the loop as a last line but as it is not there, the num is always 50 and never gets decremented so it will never become 0 so it goes in an infinite loop.
(7).
(a). Check the if condition. if(b) then continue; There is no other statement present in this condition. So, whenever value of b is non-zero which means true, the loop needs to go in the next iteration without doing anything. So, the while loop is only executed when value of b is 0 and a is non-zero. So, you can modify the statement blocks as:
while(a && b==0) // or you can also use while(a && !b)
{
c();
}
(b).
do
{
b();
c();
}
while(a);
OR
while(a)
{
b();
c();
}
So, in this case, the loop statement is not executed for a=0. See the do-while loop is at least getting executed for once for sure. For the first time, assume a=0 so if(!a) becomes true and continue; is executed so it comes to while and while(a) becomes false as a=0 so the loop here itself gets terminated. On the other node, take a as a non-zero value. Now, this if condition is false so it goes in else part and executes b() and c(). Now, it checks for a and it is non-zero so loop gets executed again. So, in the simple terms, this loop gets executed for non-zero value of a and there is no other statement inside if(), we can take that condition for while loop itself and write it as when a is non-zero, perform b() and c().
Please comment if there is any query. Thank you. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.