#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);
}
#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