C++ Program To Find Power Without Using Multiplication(*) And Division(/) Operators Last Updated : 17 Jan, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Method 1 (Using Nested Loops):We can calculate power by using repeated addition. For example to calculate 5^6. 1) First 5 times add 5, we get 25. (5^2) 2) Then 5 times add 25, we get 125. (5^3) 3) Then 5 times add 125, we get 625 (5^4) 4) Then 5 times add 625, we get 3125 (5^5) 5) Then 5 times add 3125, we get 15625 (5^6) C++ // C++ code for power function #include <bits/stdc++.h> using namespace std; // Works only if a >= 0 // and b >= 0 int pow(int a, int b) { if (b == 0) return 1; int answer = a; int increment = a; int i, j; for(i = 1; i < b; i++) { for(j = 1; j < a; j++) { answer += increment; } increment = answer; } return answer; } // Driver Code int main() { cout << pow(5, 3); return 0; } // This code is contributed by rathbhupendra Output : 125 Time Complexity: O(a * b) Auxiliary Space: O(1) Method 2 (Using Recursion) Recursively add a to get the multiplication of two numbers. And recursively multiply to get a raise to the power b. C++ // C++ program to implement // the above approach #include<bits/stdc++.h> using namespace std; // A recursive function // to get x*y int multiply(int x, int y) { if(y) return (x + multiply(x, y - 1)); else return 0; } // A recursive function to get a^b // Works only if a >= 0 and b >= 0 int pow(int a, int b) { if(b) return multiply(a, pow(a, b - 1)); else return 1; } // Driver Code int main() { cout << pow(5, 3); getchar(); return 0; } // This code is contributed by Akanksha Rai Output : 125 Time Complexity: O(b) Auxiliary Space: O(b) Method 3 (Using bit masking) we can a^n (let's say 3^5) as 3^4 * 3^0 * 3^1 = 3^, so we can represent 5 as its binary i.e. 101 C++ // C++ program to implement // the above approach #include <iostream> using namespace std; // Function calculating power long long pow(int a, int n) { int ans = 1; while(n > 0) { // Calculate last bit(right most) // bit of n int last_bit = n&1; // if last bit is 1 then multiply // ans and a if(last_bit) { ans = ans*a; } // Make a equal to square of a as on // every succeeding bit it got squared // like a^0, a^1, a^2, a^4, a^8 a = a * a; n = n >> 1; } return ans; } // Driver code int main() { cout << pow(3, 5); return 0; } Time Complexity: O(log n) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article C++ Program To Find Power Without Using Multiplication(*) And Division(/) Operators K kartik Follow Improve Article Tags : C++ C++ Basic Programs Practice Tags : CPP Similar Reads C++ Program to find whether a no is power of two Given a positive integer, write a function to find if it is a power of two or not.Examples : Input : n = 4 Output : Yes 22 = 4 Input : n = 7 Output : No Input : n = 32 Output : Yes 25 = 32 1. A simple method for this is to simply take the log of the number on base 2 and if you get an integer then nu 2 min read pow() function for complex number in C++ The pow() function for complex number is defined in the complex header file. This function is the complex version of the pow() function. This function is used to calculate the complex power of base x raised to the y-th power. Syntax: template<class T> complex<T> pow (const complex<T 2 min read valarray pow() function in C++ The pow() function is defined in valarray header file. This function returns a valarray containing the results of the power operation on all the elements, in the same order. Syntax: pow(varr, n); Parameter: varr: This represents the valarray object.n: It represents a exponent value. Returns: This fu 2 min read Power Function in C In C language, the pow() function is defined in the <math.h> header file and is used to calculate the exponent value of x raised to the power of y, i.e., xy. Basically, in C, the exponent value is calculated using the pow() function. Example: C#include <stdio.h> // Include math.h for the 3 min read scalbln() function in C++ STL The scalbln() is a built-in function in C++ STL which takes two arguments and scales x by FLT_RADIX raised to the power n. The function returns the product of x and FLT_RADIX raised to the power n. FLT_RADIX: It is the value of the radix (integer base) of the exponent representation. Syntax: scalbln 2 min read Like