2017-04-26 5 views
1

私はmath.hとpowを使わないで賃貸料を計算しようとしていますが、どういうわけか私はほとんどそれを得ましたが、正しい金額は計算されていません。私は何が足りないのですか?数学ライブラリを使わずに関心を計算する

#include <stdio.h> 

double calcFutureValue(double startingAmount, double interest, int numberOfYears); 

int main() { 

    double startMoney, interest, futureMoney; 
    int years; 

    printf("Enter amount of money: "); 
    scanf("%lf", &startMoney); 
    getchar(); 

    printf("Enter interest on your money: "); 
    scanf("%lf", &interest); 
    getchar(); 

    printf("Enter amount of years: "); 
    scanf("%d", &years); 
    getchar(); 


    futureMoney = calcFutureValue(startMoney, interest, years); 

    printf("In %d years you will have %.1f", years, futureMoney); 
    getchar(); 

    return 0; 
} 

double calcFutureValue(double startingAmount, double interest, int numberOfYears) { 

    double totalAmount; 
    double interest2 = 1; 


    for (int i = 0; i < numberOfYears; i++) 
    { 
     interest2 += interest/100; 
     totalAmount = startingAmount * interest2; 
     printf("%lf", totalAmount); 
     getchar(); 

    } 

    return totalAmount; 
} 

答えて

1

あなたの計算にcompounding the interestではありません。

あなたの機能に応じて、interest2 += interest/100

これは、10パーセントの利益のために、あなたが持っているだろうことを意味します

0 : 1 
1 : 1.1 
2 : 1.2 
3 : 1.3 

をしかし配合金利状況で、関心は以前に受取利息に適用されるだけでなく、校長:

0 : 1 
1 : 1.1 
2 : 1.21 
3 : 1.331 

このような何か試してみてください:おかげでたくさん、私はいつもdiffeを得ることが幸せです

interest2 = 1 + interest/100.0; 
totalAmount = startingAmount; 

while (numberOfYears--) { 
    totalAmount *= interest2; 
} 
+0

オースティン私はあなたが与えた解決策を試してみましたが、動作しないようです。 – Cutik

+0

'totalAmount'を初期化しましたか?更新しました。 –

+0

上記と同じです。 – Cutik

0

を私はこのかかわらを追加したときの景色を借りる、私はそれが働いた:私はループを削除し、whileループの例に置き換え、

double calcFutureValue(double startingAmount, double interest, int numberOfYears) { 

    double totalAmount; 
    double interest2 = 1; 
    double random3 = 1 + interest/100; 


    for (int i = 0; i < numberOfYears; i++) 
    { 
     interest2 *= random3; 
     totalAmount = startingAmount * interest2; 
     printf("%lf", totalAmount); 
     getchar(); 

    } 

    return totalAmount; 
関連する問題