Friday, April 6, 2012

GCD OF A NUMBER USING RECURSION

#include<stdio.h> 
#include<conio.h>

void main()
{
  int n1,n2,gcd;
  clrscr();
  printf("\nEnter two numbers: ");
  scanf("%d %d",&n1,&n2);
  gcd=findgcd(n1,n2);
  printf("\nGCD of %d and %d is: %d",n1,n2,gcd);
  getch();
}
 int findgcd(int x,int y)
{
  while(x!=y)
  {
            if(x>y)
                        return findgcd(x-y,y);
            else
                        return findgcd(x,y-x);
  }
  return x;
}

No comments:

Post a Comment