c++当中进行幂指数的计算

#include

usingstd::cin;

usingstd::cout;

intmain()

{

//局部对象

intbase, exponent;

longresult=1;

//读入底数和指数

cout<< "Enter base and exponent:" << endl;

cin>> base >> exponent;

if(exponent < 0) {

cout<< "Exponent can't be smaller than 0" << endl;

return-1;

}

if(exponent > 0) {

//计算底数的指数次方

for(int cnt = 1; cnt <= exponent; ++cnt)

result*= base;

}

cout<< base

<<" raised to the power of "

<

<

return0;

}

你可能感兴趣的:(指数函数的实现)