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

来源:互联网 发布:php 读取json 编辑:程序博客网 时间:2024/04/30 09:23

#include<iostream>

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 "

<<exponent << ": "

<<result << endl;

return0;

}

0 0
原创粉丝点击