Question Details

int x = 126, y = 105

do {

if(x > y) x = x - y;

else y = y - x;

} while(x!=y);

printf("%d", x);


The output of the given C code segment is ______.

Show Answer
Correct Answer
21

Solution :

The correct answer is 21.

Let's trace the execution of this C code segment step-by-step to understand why the output is 21.

The given code implements the subtraction-based version of Euclid's algorithm for finding the Greatest Common Divisor (GCD) of two integers, x and y.

Initially, the variables are initialized as follows:
x=126
y=105

The code uses a do-while loop, which will execute the body first and then check the condition while (x != y).

Iteration 1:
We enter the loop with x=126 and y=105.
We check the condition: if (x > y). Since 126 is greater than 105, this condition is true.
We update x:
x=x-y=126-105=21.
At the end of the iteration, we check the loop condition: while (x != y). Since x=21 and y=105, they are not equal, so the loop continues.

Iteration 2:
We enter the loop with x=21 and y=105.
We check the condition: if (x > y). Since 21 is not greater than 105, the else branch is executed.
We update y:
y=y-x=105-21=84.
We check the loop condition: while (x != y). Since x=21 and y=84 are not equal, the loop continues.

Iteration 3:
We enter the loop with x=21 and y=84.
Since x is not greater than y, we update y:
y=y-x=84-21=63.
Since x=21 and y=63 are not equal, the loop continues.

Iteration 4:
We enter the loop with x=21 and y=63.
Since x is not greater than y, we update y:
y=y-x=63-21=42.
Since x=21 and y=42 are not equal, the loop continues.

Iteration 5:
We enter the loop with x=21 and y=42.
Since x is not greater than y, we update y:
y=y-x=42-21=21.
We check the loop condition: while (x != y). Since x=21 and y=21 are now equal, the loop terminates.

After exiting the loop, the program prints the value of x using printf("%d", x);.
Since the final value of x is 21, the output printed to the console is 21.

Transparent Access on Mindyard

Built for Learners & Educators Alike

Completely free for students. Educators & coaching institutes get 2 months free Pro trial to create tests.

FREE FOR STUDENTS

Practice & Learn Free for Students

Access unlimited practice questions, detailed step-by-step solutions, and exam study materials — completely free for students.

  • Unlimited mock tests & question bank practice
  • Full access to curated study library & formula notes
  • 100% free for students • No credit card required
2 MONTHS FREE TRIAL

Educators & Coaching Institutes

Create and conduct professional online assessments with AI question generation, tab proctoring, and comprehensive batch management.

  • AI question uploader & rapid test generator
  • Anti-cheating tab proctoring & performance reports
  • Full Pro access free for 60 days • No upfront commitment
Discover more resources

You may also like

Mock Tests

View All
  • CUET UG
  • intermediate
  • 1 hour

A comprehensive CUET UG Mathematics Question Paper with Solutions – 2 June 2023 (Shift 3) designed to help students practice and understand the actual CUET exam pattern. This paper covers important topics from Algebra, Calculus, Coordinate Geometry, Probability, Statistics, Matrices, Determinants, and Vectors, featuring a balanced mix of conceptual, numerical, and application-based questions. Detailed solutions are provided to strengthen problem-solving skills, improve conceptual clarity, and enhance calculation accuracy. Based on the CUET syllabus and previous year exam trends, this paper is ideal for practice, revision, self-assessment, time management, previous year analysis, and final preparation for upcoming CUET UG Mathematics examinations.

  • CUET UG
  • intermediate
  • 1 hour

A comprehensive CUET Accountancy Question Paper – 20 June 2023 (Shift 2) designed to help students practice and analyze previous year exam patterns for effective CUET preparation. This paper covers important topics including Accounting for Partnership Firms, Company Accounts, Financial Statements Analysis, Cash Flow Statements, Accounting Ratios, and Fundamental Accounting Principles. It features a balanced mix of conceptual, numerical, and application-based questions to strengthen accounting concepts, improve calculation accuracy, and enhance problem-solving skills. Based on the CUET exam structure and syllabus, this paper is ideal for practice, revision, self-assessment, time management, previous year analysis, and final preparation for upcoming CUET Accountancy examinations.

Ask AI Tutor
5 left
Q1 View Question & Options
AI Tutor is solving this question...
Reading question context & options...
Ask your own doubt or choose a quick prompt: