Write a recursive function to calculate
((((ab)b)b)b)b…. upto n times.. don’t use pow()
or ^ operation.
#include<stdio.h>
int power(int a,int b,int n);
int main()
{
int a,b,n,y;
printf("enter the value of a,b,n resp. \n");
scanf("%d %d %d",&a,&b,&n);
y=power(a,b,n);
printf("the value of expression is %d \n",y);
return 0;
}
int power(int a,int b,int n)
{
static int f=0;
int res,i,y,k;
if(f==0)
{
res=1;
k=a;
}
else if(f<n)
{
res=1;
k=a;
}
if(f==n)
{
return a;
}
else
{
for(i=1;i<b+1;i++)
{
res=res*k;
}
f=f+1;
y=power(res,b,n);
}
return y;
}
((((ab)b)b)b)b…. upto n times.. don’t use pow()
or ^ operation.
int power(int a,int b,int n);
int main()
{
int a,b,n,y;
printf("enter the value of a,b,n resp. \n");
scanf("%d %d %d",&a,&b,&n);
y=power(a,b,n);
printf("the value of expression is %d \n",y);
return 0;
}
int power(int a,int b,int n)
{
static int f=0;
int res,i,y,k;
if(f==0)
{
res=1;
k=a;
}
else if(f<n)
{
res=1;
k=a;
}
if(f==n)
{
return a;
}
else
{
for(i=1;i<b+1;i++)
{
res=res*k;
}
f=f+1;
y=power(res,b,n);
}
return y;
}
No comments:
Post a Comment