Here is how to do simple exponential calculations using loops in C++ language.
for (int i=1;i<=b;i++)
- First we take an integer variable power and assign it a value of 1(why? this is because this variable will be involved in calculations and if we take it as 0, all the results will turn out to be 0)
- Next we take the values of two integer variables from the user. One is ‘a’ which will act as the base and the other is ‘b’ which will be the power of ‘a‘. Therefore in mathematical terms, this will look like ab .
- Now we start a for loop which will run the number of times b is. And it will store the value of calculation in the variable power. The calculation involves the multiplication of power * a until the value of i becomes greater than b(Where the loop will automatically exit).
- Next we display the value of power using cout statement.