所以我正在尝试为大学课程学习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;
}
答案 0 :(得分:1)
这是一个数学错误。如果您要将利率计算为“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。