1. 題目:a的b次方
cin 輸入練習
2. 程式碼:
// a 的 b次方 a^b
#include <iostream>
using namespace::std;
int main()
{
int a; // base
int b; // exponent
int i; // counts from 1 to y
int power; // used to calculate x raised to power y
i = 1; // initialize i to begin counting from 1
power = 1; // initialize power
cout << "輸入一個數字當作基底(base) "; // base
cin >> a; // input base cin可以從鍵盤輸入值
cout << "輸入想要的次方數(exponent) "; // exponent
cin >> b; // input exponent
// count from 1 to y and multiply power by x each time
while ( i <= b )
{
power *= a;
i++;
}
cout << power << endl;
system("PAUSE");
return 0;
}
3. 結果: