C Program to Find GCD
This is the C Program to find the gcd or hcf of two numbers,there are two methods to find gcd,subtraction method and division method
In mathematics, the greatest common divisor (gcd) of two or more integers, when at least one of them is not zero, is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4.
The GCD is also known as the greatest common factor (gcf), highest common factor (hcf), greatest common measure (gcm), or highest common divisor
Subtraction Method
#include<stdio.h>
main()
{
int num1,num2;
int res;
printf("nEnter Two Numbers n");
scanf("%d%d",&num1,&num2);
res=gcd(num1,num2);
printf("The Result is %d",res);
}
int gcd(int big,int small)
{
if(big!=small)
{
if(small<big)
{
return gcd(big-small,small);
}
else
return gcd(big,small-big);
}
else
return big;
}
Division Method:
#include<stdio.h>
main()
{
int num1,num2;
printf("Enter Two Numbers");
scanf("%d%d",&num1,&num2);
findbig(num1,num2);
}
int findbig(int num1,int num2)
{
if (num1>num2)
return gcd(num1,num2);
else
return gcd(num2,num1);
}
int gcd(int big,int small)
{
int rem;
rem=small%big;
printf("nThe Remainder is %dn",rem);
if (rem==0)
{
printf("n THE GCD is %dn",big);
}
else
return gcd(rem,big);
}
Recent Comments