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
#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));
}
}



Leave a Reply