clanguage07@gmail.com

clanguage07@gmail.com

Saturday, June 16, 2012

gcd_recursive,gcd_nonrecursive..

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

int gcd_recursive(int,int);
int gcd_nonrecursive(int,int);
void main()
{
   int a,b,ch;
   long int gcd;
   clrscr();
printf("Enter the two numbers whose GCD is to be found:");
scanf("%d%d",&a,&b);
printf("\n enter 1 for recursive 0 for nonrecursive:");
scanf("%d",&ch);
if(ch)
  gcd=gcd_recursive(a,b);
else
  gcd=gcd_nonrecursive(a,b);
printf("GCD of %d,%d is:%d",a,b,gcd);
getch();
}
int gcd_recursive(int m,int n)
{
  if(n>m)
      return gcd_recursive(n,m);
  if(n==0)
      return m;
    else
      return gcd_recursive(n,m%n);
 }
 int gcd_nonrecursive(int m,int n)
 {
    int remainder;
    remainder=m-(m/n*n);
    if(remainder==0)
       return n;
   else
      gcd_nonrecursive(n,remainder);
   }

No comments:

Post a Comment

GET MORE INFORMATION

http://ads.qadservice.com/t?id=c2168e05-8974-4816-872a-91936ff7379d&size=1024x768&drct=true