recategorized by
1,081 views
0 votes
0 votes

Consider the following C code segment:

#include 
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]

A

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

B

( j = 9!) ∧ (i =10)

C

(( j = 10!) ∧ (i = 10 )) V (( j = (x - 1)!) ∧ (i = x ))

D

(( j = 9!) ∧ (i = 10)) V (( j = (x - 1)!) ∧ (i = x ))

recategorized by

1 Answer

0 votes
0 votes

The given program calculates the factorial of a number x up to 9, i.e., it calculates 1!, 2!, 3!, ..., (x-1)!, up to a maximum of 9!. It then stops when it reaches i=x or i=10, whichever comes first.

At the end of the program, the value of j will be the factorial of (i-1) or 9, whichever comes first.

Therefore, the correct answer is (D) (( j = 9!) ∧ (i = 10)) V (( j = (x - 1)!) ∧ (i = x )).

Option (A) is incorrect because j is not necessarily equal to (x-1)!. It is the factorial of the smaller number between (x-1) and 9.

Option (B) is incorrect because the value of j can never be equal to 9! since the loop only calculates factorials up to 9.

Option (C) is incorrect because the first part of the OR condition is incorrect, as j can never be equal to 10!.

Related questions

1 votes
1 votes
1 answer
1
Rakshita Jadoun asked Feb 4, 2023
601 views
#include <stdio.h int 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;}return 0;}
0 votes
0 votes
0 answers
2
Aakanchha asked Dec 5, 2017
461 views
The solution given to this question is :How can z=5? According to me it should be 100
0 votes
0 votes
0 answers
3
Aakanchha asked Dec 5, 2017
315 views
The answer is given as (A) but according to me it should be (B). Can anyone explain please?
0 votes
0 votes
1 answer
4
Harikesh Kumar asked Dec 4, 2017
358 views
n=3a[++n]=n++ Output : a[4]=4. How?Please explain it