Five Ways to Calculate Power in C++

Problem Statement: We have to write C++ programs/code which can effectively and efficiently calculate the power of any number.

There are mainly two prerequisites to calculate the power of any number which are:

  • Base (the number whose power, we have to calculate)
  • Exponent/Power (the number up to which, power is to be calculated)

The mathematical expression to calculate the power of any number is as follows:

(a b) or (a ^ b); where a is called the base and b is called the power or exponent.

Now we will perform this mathematical calculation of power through the following C++ code/programs.

Method 1:

Using the pow() function, It is a library function that is used to calculate the power in C++. In order to use the pow() function have to import or include this library <maths.h> or <cmath> in our C++ program/code.

#include<iostream>
#include<cmath>

using namespace std;

 int main()
 {
     int x, y;

     // taking inputs for two Nos x & y
     cin>>x>>y;

     // calling the pow function
     cout<<pow(x, y);

     return 0;
 } 

Output:

5

4

625

Method 2:

In this method, we will design our own power() function to calculate the power in C++. For doing this fundamental knowledge of functions and loops (either for loop or while loop) in C++ is required. This method is known as the iterative method to calculate power. The time complexity of this method to calculate power is O(N) as the for loop will be executed N times which is quite slow.

#include<iostream>

 using namespace std;

 // defining the iterative power function

 int power(int a, int n)
 {
     int val = 1;
     for(int i = n; i > 0; i--)
         val = val * a;
     return val;
 }

 int main()
 {
    int x, y;

    // taking inputs for two Nos x & y
    cin>>x>>y;

    // calling the iterative power function
    cout<<power(x, y);

    return 0;
 }

Output:

5

3

125

Also Read Basic Calculation Based Java Programs

Method 3:

In this method, we will design our own power() function to calculate the power in C++ using the while loop. This method is also an iterative method only to calculate the power. But the time complexity of this iterative method to calculate power is O(logN) which is quite fast.

#include <iostream>

using namespace std;

// defining the optimized iterative power function

int power(int x, int y) 
 { 
     int val = 1;

     while (y > 0)
     { 
         // If y is odd then multiply x with val
         if (y % 2 != 0) 
             val = val * x; 
         y = y / 2; 
         x = x * x;
     } 
     return val; 
 }

 int main() 
 {
     int x, y;

     // taking inputs for two Nos x & y
     cin>>x>>y;

     // calling the optimized iterative power function
     cout<<power(x, y);

     return 0;
 } 

Output:

7

5

16807

Method 4:

In this method, we will use the concept of recursion and try to design a power() function in C++ that will calculate the power without using any for loop or while loop. This is known as the recursive method to calculate the power. But the time complexity of this recursive method is O(N) which is quite slow as discussed above in method 2. In this method, we are simply dividing the calculations into two halves as the power() function is called two times within the return statement (two recursive calls).

#include<iostream>

using namespace std;

// defining the recursive power function

int power(int x, int y)
 {
     if(y == 0)
         return 1;
     else if(y % 2 == 0)
         return (power(x, y / 2) * power(x, y / 2));
     else
         return (power(x, y / 2) * power(x, y / 2) * x);
 }

 int main()
 {
     int a, b;

     // taking inputs for two Nos a & b
     cin>>a>>b;

     // calling the recursive power function
     cout<<power(a, b);

     return 0;
 } 

Output:

2

10

1024

Method 5:

In this method also we will use the concept of recursion and try to design a power() function in C++ that will calculate the power. This is again a recursive method to calculate the power. But the time complexity of this recursive method is O(logN) which is quite fast as discussed above in method 3. In this method, we are calling the power function only once within the function body (only one recursive call).

#include<iostream>

using namespace std;

// defining the optimized recursive power function

int power(int x, int y)
 {
     if(y == 0)
         return 1;
     int val = power(x, y/2);
         if(y % 2 == 0)
             return (val * val);
         else
             return (val * val * x);
 }

 int main()
 {
     int a, b;

     // taking inputs for two Nos a & b
     cin>>a>>b;

     // calling the optimized recursive power function
     cout<<power(a, b);

     return 0;
 } 

Output:

3

5

243

NOTE: Till now whatever we have discussed, is valid or true only for the integral bases (i.e. -2, 0, 3, 5) and positive powers or exponents (i.e. 0, 1, 3, 4, 5).

Bonus: Generalizing the power() function in C++

Let’s extend our approach to design a power() function that can calculate the power where bases can be floating-point numbers and exponents can be negative. Let’s code it using the recursive method. We can call it a generic method to calculate power in C++.

#include<iostream>

using namespace std;

// defining the generic recursive power function

float power(float x, int y) 
 { 
     float val; 
     if(y == 0) 
         return 1; 
     val = power(x, y / 2); 
     if (y % 2 == 0) 
         return val * val; 
     else
     { 
         if(y > 0) 
             return x * val * val; 
         else
             return (val * val) / x; 
     } 
 } 
  
 int main()
 {
     float a;
     int b;

     // taking inputs for two Nos a & b
     cin>>a>>b;

     // calling the generic recursive power function
     cout<<power(a, b);

     return 0;
 } 

Output:

9

3

729

Related posts

File Permissions in Linux

Implementing Stacks and Queues

SQL Processing and understanding its Key Capabilities