retagged by
6,498 views
4 votes
4 votes

Consider the following $\mathrm{C}$ program:

#include <stdio.h>

int main() {

int a=6;

int b = 0;

while (a<10)   {

a = a / 12+1 ;

a += b ;}

printf ("%d", a);

return 0 ; }

Which one of the following statements is CORRECT?

  1. The program prints $9$ as output
  2. The program prints $10$ as output
  3. The program gets stuck in an infinite loop
  4. The program prints $6$ as output
retagged by

3 Answers

1 votes
1 votes

The initial value is $a=6,b=0$. When the first time while loop is runs the conditions become true as $(6<10)$. Now control enters inside the for loops and executes the following lines:

$a=a/12+1\implies (6/12)+1=0+1=1$: it returns $a=1$

$a=a+b \implies1+0=1$: it returns again $a=1$

Now in the second iteration again while conditions become true as $(1<10)$ and again control enters inside the loops and executes the inside statements:

$a=a/12+1$: it returns $a=1$

$a=a+b$: it returns again $a=1$

If we execute again and again every time conditions become true. So no output is printed and programs go into infinite loops.

0 votes
0 votes
In 1st iteration,

a = 6/10 +1
a += 0
Since a is an integer, a = 1 at the end of first iteration

In 2nd iteration,

a = 1/10 +1
a += 0
Since a is an integer, a = 1 at the end of second iteration

So, value of a will always be less than 10, the while loop will never end.

Answer: option C
0 votes
0 votes

Inside the while loop a is a dependent variable that while loop will use for it's recurrence.

and we have a as,

a = a/12 + 1;

a += b; 

with a = 6 and b = 0 as initial values.

so, a = 6/12 + 1 = 0.5 + 1 = 0 + 1 [because a is an integer variable where any values after decimal point are simply dropped.]

and then a = 1 + 0 = 1

similarly more runs of a will make the value of a as 1 only. Because of the integer division.

Hence the program will never end. Option C.

Answer:

Related questions

3.5k
views
3 answers
2 votes
Arjun asked Feb 16
3,478 views
​​​Consider the following $\mathrm{C}$ program:#include <stdio.h void fX (); int main(){ fX(); return 0 };void fX () { char a; if ((a=g e t c h a r()) ! = '\n') ...
2.6k
views
2 answers
2 votes
Arjun asked Feb 16
2,603 views
​​Consider the following $\mathrm{C}$ function definition.int f (int x, int y){ for (int i=0 ; i<y ; i++ ) { x= x + x + y; } return x; }Which of the following stateme...
578
views
1 answers
8 votes
GO Classes asked Apr 18, 2022
578 views
What is the output of the following code? Assume that int is $32$ bits, short is $16$ bits, and the representation is two’s complement.unsigned int x = 0xDEADBEEF; unsi...