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
- Read two integers a and b. If either is not positive, print a message and stop.
- Recursive function
gcd_recursive(a, b): if b is 0, return a. Otherwise returngcd_recursive(b, a % b). - 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. - Call both functions and print both answers.
Program
#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
Enter two positive integers: 48 18
GCD of 48 and 18 using recursion = 6
GCD of 48 and 18 using non-recursion = 6Smaller number first
Enter two positive integers: 36 84
GCD of 36 and 84 using recursion = 12
GCD of 36 and 84 using non-recursion = 12Numbers with no common factor
Enter two positive integers: 17 5
GCD of 17 and 5 using recursion = 1
GCD of 17 and 5 using non-recursion = 1Recorded 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)orgcd_recursive(a % b, b). The arguments must be(b, a % b), in that order.Updating
abefore saving the remainder, as ina = b; b = a % b;. By thenaalready holdsb, sobbecomes 0 after one step. Saver = a % bfirst.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 % 0is 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.