+92 332 4229 857 99ProjectIdeas@Gmail.com

Power (C++)


Power
calculatePower() calculates the power of an integer positive value and it takes 2 parameter:
no takes the value whose power is to be determined.
power is the exponent value.
Both arguments to this function should be positive integers.

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"

using namespace std;

int calculatePower(int no, int power)
{
    int ans = no;
          
    if (power == 0)
              return 1;

    else if (power == 1)
              return ans;

    else
    {
       for (int i = 1; i < power; i++)
            ans = ans * no;

              return ans;
    }  
}

int main()
{
       cout<<calculatePower( 2 , 0 );cout<<endl;
       cout<<calculatePower( 2 , 1 );cout<<endl;
       cout<<calculatePower( 2 , 2 );cout<<endl;
       cout<<calculatePower( 2 , 3 );cout<<endl;
       cout<<calculatePower( 2 , 4 );cout<<endl;
       cout<<calculatePower( 2 , 5 );cout<<endl;
       cout<<calculatePower( 2 , 6 );cout<<endl;
       cout<<calculatePower( 2 , 7 );cout<<endl;
       cout<<calculatePower( 2 , 8 );cout<<endl;
       cout<<calculatePower( 2 , 9 );cout<<endl;

       _getche();
       return 0;
}

Output
1
2
4
8
16
32
64
128
256
512

0 comments: