因此,我正在尝试为大学课程学习C ++,我必须编写一个使用此公式的程序:

Amount = Principal * (1 + Rate/T)^T


其中本金是储蓄余额,利率是利率,t是一年中利息复利的次数。根据这本书,如果您输入4.25作为利率,并输入12作为本金与1000.00的复利次数,那么您应该获得43.34作为利息,总金额应为1043.34。我不确定我是否编码错误或什么错误,但是我想知道是否有人可以帮助我解决我可能犯的任何错误!我现在正尝试自己做大约一两天,但是我没有运气。

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    double PRINCIPAL;
    double INTEREST_RATE;
    double COMPOUND_AMOUNT;


    cout << "What is your savings account balance?: " << endl;
    cin >> PRINCIPAL;
    cout << "What is your annual interest rate?: " << endl;
    cin >> INTEREST_RATE;
    cout << "How many times has your interest been compounded?: " << endl;
    cin >> COMPOUND_AMOUNT;

    double amt1 = 1 + (INTEREST_RATE/COMPOUND_AMOUNT);
    double AMOUNT = PRINCIPAL * pow(amt1, COMPOUND_AMOUNT);

    cout << "Interest Rate: " << INTEREST_RATE << endl;
    cout << "Times Compounded: " << COMPOUND_AMOUNT << endl;
    cout << "Principal: " << PRINCIPAL << endl;
    cout << "Interest: " << INTEREST_RATE * COMPOUND_AMOUNT << endl;
    cout << "Amount: " << AMOUNT << endl;

    system("pause");
    return 0;
}

最佳答案

这是一个数学错误。如果将利率设为'4.25%',则需要将利率除以100。当输入4.25作为利率时,下面的代码为我提供了1043.34的金额。

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    double PRINCIPAL;
    double INTEREST_RATE;
    double COMPOUND_AMOUNT;


    cout << "What is your savings account balance?: " << endl;
    cin >> PRINCIPAL;
    cout << "What is your annual interest rate? (in %): " << endl;
    cin >> INTEREST_RATE;
    INTEREST_RATE /= 100;
    cout << "How many times has your interest been compounded?: " << endl;
    cin >> COMPOUND_AMOUNT;

    double amt1 = 1 + (INTEREST_RATE/COMPOUND_AMOUNT);
    double AMOUNT = PRINCIPAL * pow(amt1, COMPOUND_AMOUNT);

    cout << "Interest Rate (%): " << INTEREST_RATE * 100 << endl;
    cout << "Times Compounded: " << COMPOUND_AMOUNT << endl;
    cout << "Principal ($): " << PRINCIPAL << endl;
    cout << "Interest ($): " << AMOUNT - PRINCIPAL << endl;
    cout << "Amount ($): " << AMOUNT << endl;

    system("pause");
    return 0;
}


为了利息,您的书谈论的是美元的利息金额,即AMOUNT-PRINCIPAL。

关于c++ - 计算利率,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25962544/

10-11 16:18