Write a C++ to calculate power of a number (logic explained step by step)

Here is the C++ code to calculate power of a number  .It takes input for both number and its power and calculates the result step by step. You all should have in mind how it works exactly. Here is the algorithm.

  Steps
User side
1: User asked to enter a number whose power is to be calculated.
2: User asked to enter the power
3: Screen shows the result step by step
Developer side
1: Declare variables that are going to be used in program .
2: Input the number whose power is to be calculated from user
3: Input its power.
4: Pass both of the values to a function with the name 'Power_Calc()'
 5: In the function start a while loop from 1 to less then or equal to the power entered by user.
6: Here output the value of variable 'a' and do the following 
                a = a * y; (y is a variable that initially contains the value of the variable 'a')
              counter = counter + 1;
7: Return control back to the main function.
8: Program ends here.

Code:

#include<iostream>
using namespace std;
void Power_Calc(int, int);
int main() {
       int x, p;
       cout << "Enter the number = ";
       cin >> x;
       cout << "Enter the power of "<<x<<" = ";
       cin >> p;
       Power_Calc(x, p);
       system("pause");
       return 0;
}
void Power_Calc(int a, int b)
{
       int counter = 1, y = a;
       while (counter <= b)
       {
              cout<< a << endl;
              a = a * y;
              counter = counter + 1;
       }
}



Output:

Comments