Pow(x, n) Leetcode Solution in c++ | Hindi Coding Community

0

 



Implement pow(x, n), which calculates x raised to the power n (i.e., xn).

Example 1:


Input: x = 2.00000, n = 10

Output: 1024.00000



class Solution {
public:
double myPow(double x, int n) {

if(n==0 || x==1)
return 1;

double val= myPow(x,n/2);
val*=val;

if(n%2==0)
return val;

else{

if(n<0)
return val*(1/x);

return val*x;
}
}
};

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !