C GCD Program

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

What Is GCD?

The GCD (greatest common divisor), also called HCF, is the largest positive integer that divides both numbers.

Remember
gcd(a, b) = largest positive d such that
            a % d == 0 and b % d == 0

Euclid:
  while b != 0:
      (a, b) = (b, a % b)
  return a

Example: gcd(48, 18)
  48 = 18 × 2 + 12
  18 = 12 × 1 + 6
  12 = 6 × 2 + 0
  gcd = 6
abgcd
48186
17131
02121

How to Find GCD in C

Four clear steps — Euclid’s algorithm is the standard interview answer.

  1. Take absolute values so negatives do not change the GCD.
  2. While b != 0, replace (a, b) with (b, a % b).
  3. When b becomes 0, return a.
  4. Handle gcd(0, 0) as your problem requires (often return 0).
MethodIdeaTimeBest for
Iterative EuclidLoop with a % b until b == 0O(log min)Interviews, production
Recursive Euclidgcd(b, a % b) with base b == 0O(log min)Showing the recurrence

Pseudocode

Pseudocode
function Gcd(a, b):
    a = abs(a)
    b = abs(b)

    while b != 0:
        t = b
        b = a mod b
        a = t

    return a

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
MeaningLargest positive common divisor
Also calledHCF
Core step(a, b) → (b, a % b)
Stop whenb == 0; answer is a
Zerogcd(0, n) = |n| for n ≠ 0
Classic checkgcd(48, 18) = 6

Live Preview

Enter two integers to see the Euclidean steps and the GCD.

Whole numbers (absolute values used). Tap a chip or type a pair.

Result
a = 48, b = 18
48 = 18 × 2 + 12
18 = 12 × 1 + 6
12 = 6 × 2 + 0
gcd = 6

Worked Walkthrough — a = 48, b = 18

Divide, take the remainder, and swap until the remainder is zero.

Calculation
48 = 18 × 2 + 12
18 = 12 × 1 + 6
12 = 6 × 2 + 0

Stop (remainder 0)
gcd(48, 18) = 6

Check: 48 % 6 = 0 and 18 % 6 = 0 ✓

C Programs

Start with the iterative Euclidean loop. Use recursion when you want to show the recurrence clearly.

Example 1 — Iterative Euclid

Loop with remainder until b becomes zero, then return a.

C
#include <stdio.h>
#include <stdlib.h>

int gcd(int a, int b)
{
    int t;

    a = abs(a);
    b = abs(b);

    while (b != 0)
    {
        t = b;
        b = a % b;
        a = t;
    }
    return a;
}

int main(void)
{
    printf("GCD = %d\\n", gcd(48, 18));
    return 0;
}

How It Works

  1. Take absolute values so sign does not matter.
  2. While b != 0, save b in t.
  3. Set b = a % b (new remainder) and a = t.
  4. When b hits 0, a holds the GCD.
  5. For 48 and 18 the remainders are 12, then 6, then 0 → answer 6.

Example 2 — Recursive Euclid

Same algorithm as a one-line recurrence with a clear base case.

C
#include <stdio.h>
#include <stdlib.h>

int gcd(int a, int b)
{
    a = abs(a);
    b = abs(b);

    if (b == 0)
    {
        return a;
    }
    return gcd(b, a % b);
}

int main(void)
{
    printf("%d\\n", gcd(48, 18)); /* 6 */
    printf("%d\\n", gcd(0, 21));  /* 21 */
    printf("%d\\n", gcd(17, 13)); /* 1 */
    return 0;
}

How It Works

  1. Normalize with abs first.
  2. Base case: if b == 0, return a.
  3. Otherwise call gcd(b, a % b).
  4. Each call shrinks the problem — same steps as the loop.
  5. Stack depth is O(log min(a, b)); prefer the loop in production.

Edge Cases & Pitfalls

Check these before calling the solution done.

Zero

gcd(0, n) = |n|

The loop ends immediately when the other value is already 0.

Negatives

Use abs

GCD is reported as a nonnegative value.

Coprime

gcd = 1

No larger common divisor — still a correct result.

% operator

Remainder, not divide

Euclid needs a % b, not floating-point division.

Time and Space Complexity

ProgramTimeExtra space
Iterative EuclidO(log min(a, b))O(1)
Recursive EuclidO(log min(a, b))O(log min) stack

Each remainder step shrinks the numbers quickly. That is why Euclid is much faster than scanning every candidate divisor.

Key Takeaways

  • Definition: largest positive integer that divides both inputs.
  • Euclid: replace with (b, a % b) until b == 0.
  • Alias: GCD = HCF.
  • Test: (48, 18) → 6; (17, 13) → 1; (0, 21) → 21.

One line: keep taking remainders until one side is zero — that other side is the GCD.

Frequently Asked Questions

The greatest common divisor (also called HCF) of a and b is the largest positive integer that divides both.
For n > 0, gcd(0, n) = n. Many programs treat gcd(0, 0) as 0.
Replace (a, b) with (b, a % b) until b becomes 0. Then a is the GCD.
Same idea. Iteration uses O(1) extra space; recursion uses O(log min(a,b)) stack frames.
For nonnegative a and b: gcd(a, b) × lcm(a, b) = a × b (when neither is zero in the usual product form).

Next: Happy Number

Check whether a number becomes 1 via sum of squared digits.

Happy number tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful