C++ Program to Calculate Power of a Number

power of number

C++ programs to calculate the power of a number have been shown here. The power of a number is denoted by $x^n$ where $x$ is the number and $n$ is the power. It signifies that $x$ is needed to be multiplied by itself $n$ times.


In C++, the loops or inbuilt function like pow() can be used to calculate the power of a number. The recursive approach has also been shown. The algorithm, pseudocode and time complexity of the program have been shown below.






1. Algorithm to calculate the power of a number


1. Take a number x and the power n as input.

2. Multiply x with itself total n times and store the result inside a variable.

3. Display the result.




2. Pseudocode to calculate the power of a number


Input : A number $x$ and the power $n$

Output : $x^n$

1. Procedure power($x$, $n$):

2. $p \leftarrow 1$

3. Repeat for $i = 1$ to $n$

4. $p \leftarrow p * x$

5. Return $p$

6. End Procedure





3. Time complexity to calculate the power of a number


Time Complexity: O(n)

Where n is the power.





4. Program & output to calculate the power of a number




4.1. C++ Program & output to calculate the power of a number using iteration

Code has been copied
/******************************
      alphabetacoder.com
 C++ program to calculate power 
 of a number using iteration
*******************************/
#include <iostream>

using namespace std;

int main() {
    // declare variables
    int num, power, result = 1, i;

    // take input of the number & power
    cout << "Enter the number = ";
    cin >> num;
    cout << "Enter the power = ";
    cin >> power;

    // calculate the value
    for (i = 1; i <= power; i++)
        result = result * num;

    // display result
    cout << "Result of " << num << "^" << power << " = " << result << endl;

    return 0;
}

Output


Case 1:

Enter the number = 3

Enter the power = 3

Result of 3^3 = 27


Case 2:

Enter the number = 6

Enter the power = 0

Result of 6^0 = 1





4.2. C++ Program & output to calculate the power of a number using recursion

Code has been copied
/******************************
        alphabetacoder.com
 C++ program to calculate power 
 of a number using recursion
*******************************/
#include <iostream>

using namespace std;

// recursive function to calculate 
// power of a number
// here the number and power are integers
int calculate_power(int n, int power) {
    if (power < 1) // exit condition
        return 1;
    else
        return n * calculate_power(n, power - 1);
}
int main() {
    // declare variables
    int num, power, result;

    // take input of the number & power
    cout << "Enter the number = ";
    cin >> num;
    cout << "Enter the power = ";
    cin >> power;

    // calculate the value
    result = calculate_power(num, power);

    // display result
    cout << "Result of " << num << "^" << power << " = " << result << endl;

    return 0;
}

Output


Enter the number = 4

Enter the power = 3

Result of 4^3 = 64




4.3. C++ Program & output to calculate power of a number using pow()

Code has been copied
/******************************
      alphabetacoder.com
C++ program to calculate power 
of a number using pow()
*******************************/

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    // declare variables
    float num, power, result;

    // take input of the number & power
    cout << "Enter the number = ";
    cin >> num;
    cout << "Enter the power = ";
    cin >> power;

    // calculate the value
    result = pow(num, power);

    // display result
    cout << "Result of " << num << "^" << power << " = " << result << endl;

    return 0;
}

Output


Enter the number = 6.5

Enter the power = 1.5

Result of 6.5^1.5 = 16.5718