返回列表 發帖
  1. /*試著撰寫一支程式,輸入本金 p、月利率 r 及 存款期數 n,並以下列公式計算本利和 t。
  2. 其中 n 為正整數,p 、 r 、 t 皆為浮點數   t = p * (1 + r)^n*/
  3. #include <iostream>
  4. #include <cstdlib>
  5. using namespace std;

  6. float power(float x , int n){  //算x的n次方
  7.       
  8.       float num = x;  //要運算x 但不能直接運算x 所以用num = x的方式 把x傳到 num 後再做計算
  9.       for(int i=1; i<n; i++){
  10.                num = num * x;
  11.                }
  12.       return num;
  13. }

  14. int main(void){
  15.    
  16.     float p , r , t;
  17.     int n;

  18.     cout << "請輸入本金" << endl;
  19.     cin >> p;
  20.    
  21.     cout << "請輸入年利率" << endl;
  22.     cin >> r;
  23.    
  24.     cout << "請輸入存款期數" << endl;
  25.     cin >> n;

  26.     r = r + 1;  //運算公式 : t = p * (1 + r)^n

  27.     t = p * power(r , n);  

  28.     cout << "本利和為" << t << endl;
  29.    
  30.     system("pause");
  31.     return 0;
  32. }
複製代碼

TOP

返回列表