Skip to content

Instantly share code, notes, and snippets.

@capex
Created October 10, 2012 07:06
Show Gist options
  • Save capex/3863633 to your computer and use it in GitHub Desktop.
Save capex/3863633 to your computer and use it in GitHub Desktop.
GCD in C
/*
Computes greatest common divisor of two numbers.
*/
#include <stdio.h>
// function prototype
int gcd (int a, int b);
int main(int argc, char const *argv[])
{
int a, b, g;
printf("Please enter a number: \n");
scanf("%d", &a);
printf("Please enter a number: \n");
scanf("%d", &b);
g = gcd (a,b);
printf("The greatest common divisor of %d and %d is %d\n", a,b,g);
return 0;
}
int gcd (int a, int b)
{
int r; // remainder
while (b > 0)
{
r = a % b;
a = b;
b = r;
}
return a;
}
@mdamircoder
Copy link

mdamircoder commented Nov 6, 2017

Check G.C.D. / H.C.F. calculation in Python, little more easier.
For two integers : GCD.py
GCD for list of elements : GCD-of-LIST.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment