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 ______.
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, and .
Initially, the variables are initialized as follows:
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 and .
We check the condition: if (x > y). Since 126 is greater than 105, this condition is true.
We update :
.
At the end of the iteration, we check the loop condition: while (x != y). Since and , they are not equal, so the loop continues.
Iteration 2:
We enter the loop with and .
We check the condition: if (x > y). Since 21 is not greater than 105, the else branch is executed.
We update :
.
We check the loop condition: while (x != y). Since and are not equal, the loop continues.
Iteration 3:
We enter the loop with and .
Since is not greater than , we update :
.
Since and are not equal, the loop continues.
Iteration 4:
We enter the loop with and .
Since is not greater than , we update :
.
Since and are not equal, the loop continues.
Iteration 5:
We enter the loop with and .
Since is not greater than , we update :
.
We check the loop condition: while (x != y). Since and are now equal, the loop terminates.
After exiting the loop, the program prints the value of using printf("%d", x);.
Since the final value of is 21, the output printed to the console is 21.
Completely free for students. Educators & coaching institutes get 2 months free Pro trial to create tests.
Access unlimited practice questions, detailed step-by-step solutions, and exam study materials — completely free for students.
Create and conduct professional online assessments with AI question generation, tab proctoring, and comprehensive batch management.
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.
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.
Copyright © 2026 Combe Labs Private Limited. All Rights Reserved.