In: Computer Science
Write a C function gcd that returns the greatest common divisor of two integers. The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the two numbers.
//c program to find gcd of two numbers
#include<stdio.h>
int main()
{
int p,q;
// scanning two numbers from user
printf("Enter first number : ");
scanf("%d",&p);
printf("Enter second number : ");
scanf("%d",&q);
printf("gcd of %d and %d is %d",p,q,gcd(p,q));
}
//function for gcd
int gcd(int r,int s)
{
//if one of two numbers is zero remaining number is
gcd.
if(r==0)
return s;
if(s==0)
return r;
//if both numbers are equal,that number is gcd.
if(r==s)
return r;
if(r>s)
return gcd(r-s,s);
if(r<s)
return gcd(r,s-r);
}