Created
October 10, 2012 07:06
-
-
Save capex/3863633 to your computer and use it in GitHub Desktop.
GCD in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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