the question is to calculate x^n(x power n) (whr n can be +ve or -ve) by using recursion. the algorithm is
x= 1, n=0
1/x^n, n<0
x*x^(n-1), n>0
ive written the program it runs but im not having the correct values..here is the code:
i knw the problem comes frm the foll part
else if (n<0)
return (1/(x*power(n)));
else
return (x*(x*power(n-1)));
i shud not multiply x by power() but thn hw do i do it??? if i dont multiply i just put it like that
else if (n<0)
return (1/(x power(n)));
else
return (x*(x power(n-1)));
i get error!!
plzzz help
x= 1, n=0
1/x^n, n<0
x*x^(n-1), n>0
ive written the program it runs but im not having the correct values..here is the code:
Code:
#include<iostream.h> #include<conio.h> int power(int n); int x,n; void main() {clrscr(); cout<<"Input a value for x"<<endl; cin>>x; cout<<"\n Input a value for n"<<endl; cin>>n; cout<<"The final answer is"<<power(n); getch(); } int power(int n) { if (n==0) return 1; else if (n<0) return (1/(x*power(n))); else return (x*(x*power(n-1))); }
i knw the problem comes frm the foll part
else if (n<0)
return (1/(x*power(n)));
else
return (x*(x*power(n-1)));
i shud not multiply x by power() but thn hw do i do it??? if i dont multiply i just put it like that
else if (n<0)
return (1/(x power(n)));
else
return (x*(x power(n-1)));
i get error!!
plzzz help
Comment