Can someone show me what I am doing wrong my C++ code the calculations are worng
ID: 3665422 • Letter: C
Question
Can someone show me what I am doing wrong my C++ code the calculations are worng.
Here is the question:
Write a function declaration for a function that computes interest on a credit card account balace. The function takes arguments for the intitial balance, the monthly intrest rate, and the number of months for which interest must be paid. The vakue returned is the interest due. Do not forget to compound the interst-that is, to charge inteest on the interest due. The interest due is added to the balance due, and the interest and interest for the next month is computed using this larger balance. Use a while loop that is similar to (but need not be identical to) the one shown in Display 2.14:
A Loop Body with Several Statements:
do
{
Statement_1
Statement_2
…
Statement_Last
} while (Boolean _Expression);
A Loop Body with a Single Statement:
do
Statement
while (Boolean_Expression);
Embed the function in a progrsm that reads the value for the interest rate, initial account balance, and number of months, then out puts the interest due. Embed your function definition in a program that lets the user compute interest due on a credit account balance. the program should allow the user to repeat the calculation until the user says he or she wants to end the program.
Here is my code:
#include<iostream>
using namespace std;
// Function definition
double interest (double , double , int );
int main()
{
// Decleration of veriables.
double Balance, interestRate, total;
int months;
char choice;
do
{
// Inputting data that needs to be calculated.
cout<<"Credit card interest."<<endl;
cout<<"Enter doubles: initial balance, monthly interest rate as"<<endl;
cout<<"a decimal faction, e.g. for 1.5% per month write 0.015"<<endl;
cout<<"and int months the bill has run."<<endl;
cout<<"I will give you the interest that has accumulated."<<endl;
cin>>Balance;
cin>>interestRate;
cin>>months;
// Call the function.
total=interest(Balance, interestRate, months);
// Outputting interest.
cout<<"Interest accumulated = : "<<total<<endl;
// Input chice to repeat.
cout<<"Y or y repeats, any other character quits.";
cin>>choice;
}
while(choice == 'y' || choice == 'Y');
// Pause system for a while
// system("pause");
}
// Function definition
double interest (double intitialBalance, double interestRate, int months)
{
double initialBalance;
double total = 0;
int i;
for (i = months; i>=0; i--)
{
total += ((initialBalance * interestRate)/(100 *12));
initialBalance += ((initialBalance *(100+interestRate))/(100 *12));
}
return total;
}
The output should look like this:
Explanation / Answer
Here the correct answer is
#include <iostream>
using namespace std;
double CompoundInterest(double balance,double months,double rate);
int main()
{
double months,rate,balance;
cout<<"Credit card interest."<<endl;
cout<<"Enter doubles: initial balance, monthly interest rate as"<<endl;
cout<<"a decimal faction, e.g. for 1.5% per month write 0.015"<<endl;
cout<<"and int months the bill has run."<<endl;
cout<<"I will give you the interest that has accumulated."<<endl;
cin>>balance;
cin>>rate;
cin>>months;
cout << endl << "Payment due is : " << CompoundInterest(balance,months,rate);
return 0;
}
double CompoundInterest(double balance,double months,double rate)
{
for(int i = 0;i < months;i++)
{
double balanceDue,interestDue;
balanceDue = balance + (balance * rate/100);
interestDue = balanceDue - balance;
balance = balanceDue;
}
return balance;
}