in Programming in C recategorized by
4,329 views
5 votes
5 votes

Consider the following C code segment:

#include <stdio.h>
main()
{
    int i, j, x;
    scanf("%d", &x);
    i=1; j=1;
    while (i<10) {
            j =j*i;
            i= i+1;
            if(i==x) break;
        }
}

For the program fragment above, which of the following statements about the variables i and j must be true after execution of this program? [ !(exclamation) sign denotes factorial in the answer]

  1. $(j=(x-1)!)$ $\wedge$ $(i$$\geq x$$)$
  2. $(j=9!)$ $\wedge$  $(j=10)$
  3. $((j=10!)$ $\wedge$ $(i=10))$ $\vee$ $((j=(x-1)!)$ $\wedge$ $(i=x))$
  4. $(j=9!)$ $\wedge$$ (i\geqslant$$10$)) $\vee$ $((j=(x-1)!) $$\wedge$$ (i=x))$
in Programming in C recategorized by
by
4.3k views

3 Comments

edited by

In the loop when, $i =n$ the value of $j = \prod_{i=1}^{n-1}  i= (n-1)!$

Loop will terminate when $i=x$ or $i=10$

$\textit{1.}$ for $i=x$ the value of $j=\prod_{i=1}^{x-1}  i = (x-1)!$ $\equiv ((j=(x-1)!) \wedge(i=x))$


$\textit{2.} $for $i=10$ the value of $j=\prod_{i=1}^{10-1}i=(10-1)! \equiv ((j=9!) \wedge(i\geqslant10))$

taking both we have,

$((j=9!) \wedge(i\geqslant10))\vee ((j=(x-1)!) \wedge(i=x)) $

Option - D

0
0
after execution of the program i will b 10. it is asked that which of the options will be true for i and j so i>=10 will be true for i=10
0
0
And also when suppose while loop is breaked when i=x at time also the j would be equal to (x-1)!  And so

(j=(x-1)!)^(i>=x)

In this since i=x so i>=x is true and so j=(x-1)! Is also true so why this statement is not correct ?
0
0

1 Answer

8 votes
8 votes
Best answer
option D is correct

when value of x>= 10 then value of j will be 9! because condition of while loop is (i<10) .so i will run till 9 and j value will be computed as 9!

and if x<10 then loop will terminate when i will be incremented up to x.So that time j value will be computed as (x-1)!
selected by

1 comment

It is false when x is assigned a negative integer. Then the program runs for (2x-1)! times if x is negative
0
0
Answer:

Related questions