Bird
0
0
DSA Cprogramming~20 mins

GCD and LCM Euclidean Algorithm in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Euclidean Algorithm Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of GCD calculation using Euclidean Algorithm
What is the output of the following C code that calculates the GCD of 48 and 18 using the Euclidean Algorithm?
DSA C
int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int main() {
    int result = gcd(48, 18);
    printf("%d\n", result);
    return 0;
}
A9
B12
C3
D6
Attempts:
2 left
💡 Hint
Remember the Euclidean Algorithm repeatedly replaces the larger number by the remainder of the division until the remainder is zero.
Predict Output
intermediate
2:00remaining
Output of LCM calculation using GCD
What is the output of the following C code that calculates the LCM of 15 and 20 using the GCD function?
DSA C
int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int lcm(int a, int b) {
    return (a / gcd(a, b)) * b;
}

int main() {
    int result = lcm(15, 20);
    printf("%d\n", result);
    return 0;
}
A100
B60
C35
D300
Attempts:
2 left
💡 Hint
LCM can be calculated using the formula: LCM(a,b) = (a * b) / GCD(a,b).
🧠 Conceptual
advanced
2:00remaining
Why does the Euclidean Algorithm work for GCD?
Which of the following best explains why the Euclidean Algorithm correctly computes the GCD of two numbers?
ABecause the algorithm multiplies the two numbers until they become equal, which is the GCD.
BBecause it adds the two numbers repeatedly until the sum is divisible by both numbers.
CBecause the GCD of two numbers also divides their difference, so repeatedly replacing the larger number by the remainder preserves the GCD.
DBecause it finds the smallest number that divides both numbers by checking all numbers from 1 upwards.
Attempts:
2 left
💡 Hint
Think about how divisors relate to remainders.
🔧 Debug
advanced
2:00remaining
Identify the error in this GCD function implementation
What error will occur when running this C code to compute GCD?
DSA C
int gcd(int a, int b) {
    while (a != 0) {
        int temp = a;
        a = b % a;
        b = temp;
    }
    return b;
}

int main() {
    int result = gcd(48, 18);
    printf("%d\n", result);
    return 0;
}
ANo error; output is 6
BRuntime error: division by zero
CInfinite loop
DCompilation error
Attempts:
2 left
💡 Hint
Check the loop condition and the order of variables in the modulo operation.
🚀 Application
expert
2:00remaining
Number of steps in Euclidean Algorithm for GCD(1071, 462)
How many iterations does the Euclidean Algorithm take to compute GCD of 1071 and 462 using the standard while loop method?
DSA C
int gcd(int a, int b) {
    int count = 0;
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
        count++;
    }
    return count;
}

int main() {
    int steps = gcd(1071, 462);
    printf("%d\n", steps);
    return 0;
}
A3
B4
C5
D6
Attempts:
2 left
💡 Hint
Perform the Euclidean Algorithm step by step counting each iteration.