Sunday, May 22, 2011

Write a recursive program for calculating ab or a-b and also find the greatest common divisor of 2 positive integers with the help of Euclid’s algorithm. The definition of Euclid’s algorithm is ---- GCD(n,m) –{m if n>m sw n mod m=0 {Otherwise GCD(m,n mod m)

#include /*Header File*/
#include /*Header File*/

int a,b,c,m,n,l,k,num; /*Declaration of variables*/
int recursion(int m,int n); /*Fuunction declaration*/
int GCD(int l,int k); /*Fuunction declaration*/

main()
{
int choice,g1; /*Declaration of variables*/
printf("\nEnter the value of a and b:\n");
printf("a=");
scanf("%d",&a);
printf("b=");
scanf("%d",&b);
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\ncalling of recyoursive ():\n");
num=recursion(a,b);
printf("The recursive value is:%d",num);
break;
case 2: printf("Calling of GCD function:\n");
g1=GCD(a,b);
printf("%d",g1);
break;
default: printf("Wrong choice");
}

}

int recursion(int m,int n) /*Function definition*/
{
int count;
for(count=1;count {
m=(m*a);
}
return m;
}

int GCD(int l,int k) /*Function definition*/
{
int c=0;
int g=0;
c=k%l;
printf("c=%d",c);

if((c==0)&&(l {
g=l;
printf("\ngcd=");
}
else
GCD(c,l);
return g;
}


OUTPUT

Enter the value of a and b:
a=2
b=8

Enter your choice: 2
Calling of GCD function:
c=0
gcd=2

No comments:

Post a Comment