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 SNIPPETSource 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.
- Declare the string exponent as an integer and the string base, result as float values. Initialize the float value as 1.
- Collect the values from the user and store it in the integer and the float values using function
cin>>
 and display the value usingcout<<
 and the Insertion Operators'<<‘ , ‘>>’. -  Create an output statement as follows Â
cout <<"\n"<< base << " ^ " << exponent << " = ";
to display the answer. - Create a while loop with the condition that the exponent value should not be equal to 0.
- Multiply the value of  result and the base using the Multiply AND assignment operator and store the answer in result.
- Next we decrement the value of exponent. The loop statement is executed multiple times till the condition is false.
- 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
- 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 usingcout<<
 and the Insertion Operators'<<‘ , ‘>>’. -  Next we create a for loop and declare the condition to be satisfied.
- In the loop statement using the Multiply AND assignment operator, multiply the value of result and the base and store the answer in result.
- The loop is executed multiple times till the condition is not satisfied and the execution is terminated.
- The rest of the steps are similar to Calculate the Power of a Number.