Share with others

# GCD of two integers Code in C Language

Write a GCD of two integers program in C to compute the GCD of the given two integers .User need to enter two numbers a and b,which needs to be use to find GCD of two numbers.

You can take the example, the GCD(**greatest common divisor**) of 8 and 12 is 4.

## GCD of two integers Code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include<stdio.h> int gcd(int a, int b); int gcd_recurse(int a, int b); int main() { int a,b; clrscr(); printf(“\nenter two nos to caluculate GCD:”); scanf(“%d%d”,&a,&b); printf(“\nGCD(%2d,%2d) = [%d]“, a,b, gcd(a,b)); printf(“\nGCD(%2d,%2d) = [%d]“, a,b, gcd_recurse(a,b)); getch(); return(0); } int gcd(int a, int b) { int temp; while(b) { temp = a % b; a = b; b = temp; } return(a); } int gcd_recurse(int a, int b) { int temp; temp = a % b; if (temp == 0) { return(b); } else { return(gcd_recurse(b, temp)); } } |

Share with others