Calculating GCD of two numbers

Problem Statement
Calculate GCD of two numbers using recursion
#include<stdio.h>
int gcd(int a,int b);
int main()
{
  int a,b,y,temp;
  printf("enter the value of a and b \n");
  scanf("%d %d",&a ,&b);
  if(a>b)
    {
      temp=b;
      b=a;
      a=temp;
    }
  y=gcd(a,b);
  printf("the gcd of giwen two numbers is %d \n",y);
  return 0;
}
int gcd(int a,int b)
{
  int temp,y;
  if(b%a==0)
    {
      return a;
    }
  else
    {
      temp= b%a;
      b=a;
      a=temp;    
      y=gcd(a,b);
    }
  return y;
}

No comments:

Post a Comment