C++ Program to Calculate the Power of a Number

In this post, you will learn how to Calculate the Power of a Number using C++ programming language.

This lesson will teach you how to calculate the power of a number, with a while loop, decrement operator and mathematical functions using the C++ Language. Let’s look at the below source code.

How to Calculate the Power of a Number?

RUN CODE SNIPPET

Source Code

#include <iostream>
using namespace std;
int main() 
{
    int exponent;
    float base, result = 1;
    cin >> base >> exponent;
    cout <<"Enter values for base and exponent: "<<base<<","<<exponent<<endl;
    cout <<"\n"<< base << " ^ " << exponent << " = ";
    while (exponent != 0) 
    {
        result *= base;
        – exponent;
    }
    cout << result;
    return 0;
}

Input

5
2

Output

Enter values for base and exponent: 5,2
5 ^ 2 = 25

The statements #include<iostream>, using namespace std, int main are the main factors that support the function of the source code. Now we can look into the working and layout of the code’s function.

  1. Declare the string exponent  as an integer and the string base, result as float values. Initialize the float value as 1.
  2. Collect the values from the user and store it in the integer and the float values using function cin>>  and display the value using cout<< and the Insertion Operators'<<‘ , ‘>>’.
  3.  Create an output statement as follows  cout <<"\n"<< base << " ^ " << exponent << " = "; to display the answer.
  4. Create a while loop with the condition that the exponent value should not be equal to 0.
  5. Multiply the value of  result and the base using the Multiply AND assignment operator and store the answer in result.
  6. Next we decrement the value of exponent. The loop statement is executed multiple times till the condition is false.
  7. Finally the output statement is displayed and the execution is terminated.

Note: The ‘ << endl ‘ in the code is used to end the current line and move to the next line and ‘\n’ is also a new line function, to understand how both the functions work exclude it from the code, move it around and work with it.

Additional Source Code

You can also try the source code given below. The following code is structured using for loop, decrement operator and mathematical functions using the C++ Language. Let’s look at the below source code.

RUN CODE SNIPPET
#include<iostream>
using namespace std;
int main()
{
  int base, exponent, i;
  long result=1;
  cin >> base >> exponent;
  cout << "Enter base and exponent: "<<base<<","<<exponent<<endl;
  for (i = exponent; i >= 1; i--)
  {
    result *= base;
  }
  cout<<"\n"<< base << " ^ " << exponent << " = "<< result << endl;
  return 0;
}

Input

5
2

Output

Enter base and exponent: 5,2
5 ^ 2 = 25
  1. Similar to the previous code we declare the string values as integers and collect the respective values from the user using the function cin>>  and display the value using cout<< and the Insertion Operators'<<‘ , ‘>>’.
  2.  Next we create a for loop  and declare the condition to be satisfied.
  3. In the loop statement using the Multiply AND assignment operator, multiply the value of  result and the base and store the answer in result.
  4. The loop is executed multiple times till the condition is not satisfied and the execution is terminated.
  5. The rest of the steps are similar to Calculate the Power of a Number.