返回列表 發帖

求本金利息和問題

試著撰寫一支程式,輸入本金 p、月利率 r 及 存款期數 n,並以下列公式計算本利和 t。
其中 n 為正整數,p 、 r 、 t 皆為浮點數
t = p * (1 + r)^n
我是小紅老師,小紅老師是我!!

哈哈 我是第一個寫好ㄉ!!
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. float power(float x , int n){
  5.     float num = x;
  6.     for(int i = 1; i < n; i++){
  7.         num = num * x;
  8.     }
  9.     return num;
  10. }
  11. int main(void){
  12.    
  13.     float p , r , t;
  14.     int n;
  15.     cout << "請輸入本金" << endl;
  16.     cin >> p;
  17.     cout << "請輸入年利率" << endl;
  18.     cin >> r;
  19.     cout << "請輸入存款期數" << endl;
  20.     cin >> n;
  21.     r = r + 1;
  22.     t = p * power(r , n);
  23.     cout << "本利和為 " << t << endl;
  24.    
  25.     system("pause");
  26.     return 0;
  27. }
複製代碼

TOP

  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

本帖最後由 tony 於 2010-6-4 19:22 編輯

#include<iostream>
#include<cstdlib>
#include<math.h>
using namespace std;
int main(void){
    float p ;
    float r ;
    int n ;
    float t ;
    cout << "enter your money" << endl;
    cin >> p ;
    cout << "enter your bonus" << endl;
    cin >> r ;
    cout << "enter your year" << endl;
    cin >> n ;
    int nn = n * 12 ;
    if(r > 1){
         float rr = r * 0.01 ;
         cout <<  p *  pow((1 + rr) , nn) << endl ;
         }else if(r < 1){
                cout <<  p *  pow((1 + r) , nn) << endl ;
                }  
   
   
    system("pause");
    return 0 ;
}

TOP

  1. //t = p * (1 + r)^n
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <math.h>
  5. using namespace std;
  6. int main(void){

  7.    
  8.     float p , r;
  9.     int n;
  10.     cout << "請輸入本金" << endl;
  11.     cin >> p;
  12.     cout << "請輸入月利率" << endl;
  13.     cin >> r;
  14.     cout << "請輸入存款期數" << endl;
  15.     cin >> n;
  16.     cout << "本利和為 " << p * pow((r+1) , n) << endl;
  17.    

  18.     system("pause");
  19.     return 0;
  20. }
複製代碼
明輝

TOP

返回列表