c++當中進行冪指數的計算

#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;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章