Programming-for-Output-Problems
October 6, 2023Programming-for-Output-Problems
October 6, 2023Programming-for-Output-Problems
Question 7 |
How many lines of output does the following C code produce?
#include<stdio.h>
float i=2.0;
float j=1.0;
float sum = 0.0;
main()
{
while (i/j > 0.001)
{
j+=j;
sum=sum+(i/j);
printf(“%f\n”, sum);
}
}
#include<stdio.h>
float i=2.0;
float j=1.0;
float sum = 0.0;
main()
{
while (i/j > 0.001)
{
j+=j;
sum=sum+(i/j);
printf(“%f\n”, sum);
}
}
8 | |
9 | |
10 | |
11 |
Question 7 Explanation:
Iteration-1:
while (1.000000 > 0.001)
{
j=2.0
sum=0+1.000000;
printf(“%f\n”,sum); /* It will print 1.000000 */
}
while (1.000000 > 0.001)
{
j=2.0
sum=0+1.000000;
printf(“%f\n”,sum); /* It will print 1.000000 */
}
Iteration-2: 1.500000
Iteration-3: 1.750000
Iteration-4: 1.875000
Iteration-5: 1.937500
Iteration-6: 1.968750
Iteration-7: 1.984375
Iteration-8: 1.992188
Iteration-9: 1.996094
Iteration-10: 1.998047
Iteration-11: 1.999023
The program will terminate after 11th iteration. So, it print 11 lines.
Correct Answer: D
Question 7 Explanation:
Iteration-1:
while (1.000000 > 0.001)
{
j=2.0
sum=0+1.000000;
printf(“%f\n”,sum); /* It will print 1.000000 */
}
while (1.000000 > 0.001)
{
j=2.0
sum=0+1.000000;
printf(“%f\n”,sum); /* It will print 1.000000 */
}
Iteration-2: 1.500000
Iteration-3: 1.750000
Iteration-4: 1.875000
Iteration-5: 1.937500
Iteration-6: 1.968750
Iteration-7: 1.984375
Iteration-8: 1.992188
Iteration-9: 1.996094
Iteration-10: 1.998047
Iteration-11: 1.999023
The program will terminate after 11th iteration. So, it print 11 lines.
Subscribe
Login
0 Comments