Simple exponential calculations in C++

Here is how to do simple exponential calculations using loops in C++ language.

[sourcecode language='cpp']
#include
void main()
{
int power=1;
int a,b;
cin>>a>>b;
for (int i=1;i<=b;i++)
{
power *=a;
}
cout< }

[/sourcecode]

Instructions:

  • 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.

  • A Stranger Passing

    Interesting stuff, but pardon me for trying to follow the logic…

    But this dont work when I try to apply this void function as a case to my main function, as is.
    any ideas on how to make it work?