In: Computer Science
I wrote a c++ program that is suppose to calculate the interest on a CD account but, I cant get the formula to calculate it correctly. please help.
#include <iostream>
#include <cmath>
using namespace std;
struct account
{
double balance;
double interest_rate;
int term;
};
void info(account& accountinfo);
int main(void)
{
double calc1, calc2;
account accountinfo;
info(accountinfo);
calc1 = (accountinfo.interest_rate / 100) + 1;
calc2 = pow(calc1, accountinfo.term);
accountinfo.balance = accountinfo.balance * calc2;
cout << " " << endl
<< " The balance of your CD account after it has matured in " << accountinfo.term << endl
<< " at a interest rate of " << accountinfo.interest_rate << " will be " << accountinfo.balance << endl
<< " " << endl;
return 0;
}
void info(account& accountinfo)
{
cout << " " << endl
<< " This program calculates the value of a CD after maturity. " << endl
<< " " << endl
<< " Enter the balance on the account. " << endl;
cin >> accountinfo.balance;
cout << " " << endl
<< " Enter the interest rate for the CD. " << endl
<< " " << endl;
cin >> accountinfo.interest_rate;
cout << " " << endl
<< " Enter the term for the CD to achieve maturity. " << endl
<< " " << endl;
cin >> accountinfo.term;
}
The formula for calculating the CD interest is as follows:
The code what you have written is correct only.
For example:
Let's say
balance = 1000
interest rate = 2
term = 10
Then we have the interest = 1000 * ( 2/100 + 1 )10
= 1000 * (0.02 + 1 )10
= 1000 *(1.02)10
= 1000 * 1.2189944
=1218.9944
YOU DON't NEED TO CHANGE ANY CODE:
SAME CODE:
#include <iostream>
#include <cmath>
using namespace std;
struct account
{
double balance;
double interest_rate;
int term;
};
void info(account& accountinfo);
int main(void)
{
double calc1, calc2;
account accountinfo;
info(accountinfo);
calc1 = (accountinfo.interest_rate / 100) + 1;
calc2 = pow(calc1, accountinfo.term);
accountinfo.balance = accountinfo.balance * calc2;
cout << " " << endl
<< " The balance of your CD account after it has matured in " << accountinfo.term << endl
<< " at a interest rate of " << accountinfo.interest_rate << " will be " << accountinfo.balance << endl
<< " " << endl;
return 0;
}
void info(account& accountinfo)
{
cout << " " << endl
<< " This program calculates the value of a CD after maturity. " << endl
<< " " << endl
<< " Enter the balance on the account. " << endl;
cin >> accountinfo.balance;
cout << " " << endl
<< " Enter the interest rate for the CD. " << endl
<< " " << endl;
cin >> accountinfo.interest_rate;
cout << " " << endl
<< " Enter the term for the CD to achieve maturity. " << endl
<< " " << endl;
cin >> accountinfo.term;
}
================================