Sunday, November 5, 2017

Euclid's Algorithm C code

Euclid's algorithm calculates the GCD (Greatest Common Divisor) for two numbers a and b. The following C program uses recursion to get the GCD.

The code is also available on GitHub.

#include <stdio.h>

int euclid(int a, int b) {
 if (b == 0) {
  return a;
 } else {
  return euclid(b, a % b);

int main(int argc, char* argv[]) {
 int a = 30, b = 21;
 printf("GCD is %d\n", euclid(a, b));
 return 0;

No comments:

Post a Comment