Program to calculate pow(x,n) using inbuilt power function
To solve the problem follow the below idea:
We can use inbuilt power function pow(x, n) to calculate xn
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; int power( int x, int n) { // return type of pow() // function is double return ( int ) pow (x, n); } // Driver Code int main() { int x = 2; int n = 3; // Function call cout << (power(x, n)); } // This code is contributed by hemantraj712. |
8
Time Complexity: O(log n)
Auxiliary Space: O(1), for recursive call stack
C++ Program To Calculate the Power of a Number
Write a C++ program for a given two integers x and n, write a function to compute xn. We may assume that x and n are small and overflow doesn’t happen.
Examples :
Input : x = 2, n = 3
Output : 8Input : x = 7, n = 2
Output : 49