retagged by
30,286 views
41 41 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

6 Answers

33 33 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.

Option $(C)$ is correct.

edited by
3 3 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.

0 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 0 votes
answer is Option(C)

 

given a = 6

b = 0

condition is while ( a<10)

 

a = a/12+1;

a = 6/12+1 = 0.5 +1 = 0 +1 = 1 (because its integer variable s we take 0 not 0.5)

now a += b :   a = a+b = 1+0 = now value of a = 1;

now, value of a =1  which will be always be less than 10, the while loop will never ends

 
0 0 votes
here if we observe closely we find there is no increment in veriable a

so that we can not fail the loop condition thats why it run infinitely

note here there is no chance of stack over flow because there is no function call

when function call happen like this the after certain point of time stack exhusted
Answer:
Position:
Show:

Related questions

39 39 votes
4 4 answers
23.0k
23.0k views
Arjun asked Feb 16, 2024
23,046 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') fX(); ...
54 54 votes
4 answers 4 answers
19.9k
19.9k views
Arjun asked Feb 16, 2024
19,938 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 statements ...
53 53 votes
5 answers 5 answers
18.6k
18.6k views
Arjun asked Feb 16, 2024
18,569 views
​​​Consider the following $\mathrm{C}$ function definition.int fX(char *a) { char *b = a; while (*b) b++; return b - a; }Which of the following statements is/are TRUE?The...
10 10 votes
1 1 answer
2.8k
2.8k views
GO Classes asked Apr 18, 2022
2,777 views
What will be the output of the following C program?#include<stdio.h void main() { int a =0; int b = (a ? a++: a ? a++ : a ); printf("%d", b); }$0$$-1$$-2$$2$