The holder of an IRA can deposit money to the account each year. The amount is usually withdrawn when the depositor is age 65. However, it can be withdrawn when the depositor is age 59.5 without incurring a penalty. A bank wishes to show its customers the difference between what they will have in their accounts at age 65 and what they will have in their accounts at age 59.5, if equal annual payments are made to the account. Write a program to do this.
The program should prompt for the annual deposit (which should not exceed $2,000) the annual interest rate, and the customer’s age when the account was opened.
Use a function to obtain each input datum.
The function that obtains the interest rate should allow the user to enter the interest rate as a percent (for example, 4.7) but should return the decimal equivalent of the entered percent (for example 0.047).
The amount in an IRA after n years of equal annual deposits of R dollars at an annual interest rate of r is given by the following formula.
A= R((1 + r) n -1/r)
To calculate the value of an IRA when the customer is 65, first subtract the customer’s age at the time the account was opened from 65. This gives the value of n to use in the formula. The values of R and r are obtained from the user. To calculate the value of an IRA when the customer is 59.5, proceed in the same manner, but subtract the customer’s age from 59.5 to obtain the value of n.
The program should output the value of the IRA at age 65, the value of the IRA at age 59.5 and the difference between these values.
My problem is, my code gives off weird number when calculating the annuity;
this is my code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
#include <iostream>
#include <cmath>
using namespace std;
void annualdeposite(double a);
void interestrate(double b);
double age(double c);
void annuity(); //n=years, r=interest,
double globalintrest;
double years;
double deposit;
double results1;
double results2;
double rate;
double customer65;
double customer59;
double difference;
void main()
{//variables
cout<<"Individual Retirement Account"<<endl;
annualdeposite(deposit);
interestrate(rate);
age(years);
customer65=65-years; //subtracting the year of 65
customer59=59.5-years;
annuity(); //calling annuity
system ("Pause");
}
void annualdeposite(double a) //annual
{
cout<<"Enter the annual deposit: $";
cin>>a;
deposit=a;
while (deposit > 2000)
{cout<<"ERROR: This amount is exceeding $2000. Please re-enter: $";
cin>>deposit;
}
}
void interestrate(double b) //interest function
{
cout<<"Enter the INTEREST RATE: %";
cin>>b;
globalintrest= b*0.01;
cout<<"Decimal Equivalent:"<<globalintrest;
}
double age(double c) //age function.
{
cout<<"\nEnter the customer's age when the account was opened: ";
cin>>c;
years=c;
return years;
}
void annuity() //annuity function
{
results1=deposit*((pow((1+globalintrest),customer65)-1)/globalintrest);
cout<<"The IRA at age 65: $"<<results1<<endl;
results1;
results2=deposit*((pow((1+globalintrest),customer59)-1)/globalintrest);
cout<<"The IRA at age 59.9: $"<<results2<<endl;
results2;
difference=results1-results2;
cout<<"The difference between the values: $"<<difference<<endl;
difference;
}
|