Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 23

GCD of two numbers using recursive and non-recursive functions

CArrays, pointers and functionsJNTUH R22 · CS108ES

Aim

To write a C program that finds the GCD (greatest common divisor) of two given positive integers in two ways, with a recursive function and with a non-recursive function, using Euclid's method.

Algorithm

  1. Read two integers a and b. If either is not positive, print a message and stop.
  2. Recursive function gcd_recursive(a, b): if b is 0, return a. Otherwise return gcd_recursive(b, a % b).
  3. Non-recursive function gcd_iterative(a, b): while b is not 0, set r = a % b, a = b and b = r. When the loop ends, return a.
  4. Call both functions and print both answers.

Program

C
#include <stdio.h>

/* Euclid's method: gcd(a, b) = gcd(b, a % b), and gcd(a, 0) = a */
int gcd_recursive(int a, int b) {
    if (b == 0) {
        return a;
    }
    return gcd_recursive(b, a % b);
}

int gcd_iterative(int a, int b) {
    while (b != 0) {
        int r = a % b;
        a = b;
        b = r;
    }
    return a;
}

int main(void) {
    int a, b;
    printf("Enter two positive integers: ");
    if (scanf("%d %d", &a, &b) != 2) {
        printf("Invalid input\n");
        return 1;
    }
    if (a <= 0 || b <= 0) {
        printf("Both numbers must be positive\n");
        return 1;
    }

    printf("GCD of %d and %d using recursion     = %d\n", a, b, gcd_recursive(a, b));
    printf("GCD of %d and %d using non-recursion = %d\n", a, b, gcd_iterative(a, b));
    return 0;
}

Output

Numbers with a common factor

Output 1
Enter two positive integers: 48 18
GCD of 48 and 18 using recursion     = 6
GCD of 48 and 18 using non-recursion = 6

Smaller number first

Output 2
Enter two positive integers: 36 84
GCD of 36 and 84 using recursion     = 12
GCD of 36 and 84 using non-recursion = 12

Numbers with no common factor

Output 3
Enter two positive integers: 17 5
GCD of 17 and 5 using recursion     = 1
GCD of 17 and 5 using non-recursion = 1

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

Euclid's method works because any number that divides both a and b also divides a % b. For 48 and 18: gcd(48, 18) → gcd(18, 12) → gcd(12, 6) → gcd(6, 0) = 6.

The order of the two numbers does not matter. If a < b, then a % b is a, so the first step simply swaps them: gcd(36, 84) → gcd(84, 36).

Two numbers whose GCD is 1, like 17 and 5, are called co-prime.

Common mistakes

  • Writing the recursive call as gcd_recursive(a, a % b) or gcd_recursive(a % b, b). The arguments must be (b, a % b), in that order.

  • Updating a before saving the remainder, as in a = b; b = a % b;. By then a already holds b, so b becomes 0 after one step. Save r = a % b first.

  • Using the slow method of trying every number from 1 up to the smaller value. It works, but Euclid's method needs far fewer steps.

  • Not handling 0 or negative input. a % 0 is division by zero and crashes the program, so check the input first.

Viva questions

Try to answer each one aloud before you open it.

  • What is the GCD of two numbers?

    The largest positive integer that divides both numbers without a remainder. It is also called the HCF (highest common factor).

  • State Euclid's method for finding the GCD.

    gcd(a, b) = gcd(b, a mod b), and when the second number becomes 0, the first number is the GCD.

  • What is the base case of the recursive gcd function?

    b = 0. Then the function returns a without calling itself again.

  • How do you find the LCM once you know the GCD?

    LCM(a, b) = (a × b) / GCD(a, b). For 48 and 18 that is 864 / 6 = 144.

  • What are co-prime numbers?

    Two numbers whose GCD is 1, such as 17 and 5. They have no common factor other than 1.

Learn the concepts

All Programming for Problem Solving Lab experiments →