close

1.  題目:ab次方
                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.  結果:

 

​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

arrow
arrow
    全站熱搜

    布拉怡 發表在 痞客邦 留言(0) 人氣()