Question:

Consider the following code:

main() {
  int x = 126, y = 105;
  { 
    if (x > y) 
      x = x - y;
    else 
      y = y - x;
  }
  while (x != y) 
    printf("%d", x);
}
  

Show Hint

The key idea here is to note that the while loop continues printing the value of \( x \) until \( x \) equals \( y \). In this case, \( x \) will never equal \( y \), and we are stuck in an infinite loop.
Updated On: Feb 14, 2025
  • 21
  • 105
  • 126
  • 0
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation

Step 1: Understanding the code.

- We are given two integers \( x = 126 \) and \( y = 105 \).
- In the block of code, the following condition checks if \( x \) is greater than \( y \):
- Since \( x = 126 \) and \( y = 105 \), \( x>y \) holds true.
Therefore, \( x = x - y \), i.e., \( x = 126 - 105 = 21 \).

Step 2: The while loop.


- Now, we have \( x = 21 \) and \( y = 105 \).
- The while loop runs as long as \( x \neq y \). Since \( x = 21 \) and \( y = 105 \), the loop will print the value of \( x \)
continuously, and the output will be `21`.

Thus, the correct answer is \( 21 \).
Was this answer helpful?
0
0

Top Questions on Programming in C

View More Questions