In: Computer Science
Hello, I am writing the initial algorithm and refined algorithm for a program. I just need to make sure I did it correctly. I'm not sure if my formula is correct. I appreciate any help, thank you!
TASK: Write a program that will calculate the final balance in an investment account. The user will supply the initial deposit, the annual interest rate, and the number of years invested.
Solution
Description: Write a program that calculates the final balance in an investment account
Initial Algorithm:
Declare variables
Get initial deposit
Get annual interest rate
Get number of years invested
Calculate final balance
Display final balance in investment account
Data Requirements:(I/O)
Input – deposit
Input – interest rate
Input – number of years invested
Output – final balance
Formulas:
deposit * interest rate = growth rate
number of years * growth rate = final investment
Refined Algorithm:
Declare float deposit
Declare float interest rate
Declare float number of years
Declare float final balance
Display “enter initial deposit”
Input deposit
Display “enter interest rate”
Input interest rate
Set growth rate = deposit * interest rate
Set final investment = number of years * growth rate
Display “The final balance of your account will be”, final investment, “after”, number of years, “years”, EOL
I WROTE THE CODE IN C++ LANGUAGE
I ADD COMMENTS TO THE CODE FOR CLEAR UNDERSTANDING
CODE
#include <iostream>
using namespace std;
int main()
{
//variables declaration.
float
deposit,interest_rate,no_years,growth_rate,total_interest,fin_balance;
//scanning input.
cout<<"enter initial deposit: ";
cin>>deposit;
cout<<"enter interest rate(%): ";
cin>>interest_rate; //interest_rate is given in
percentage
cout<<"enter number of years: ";
cin>>no_years;
growth_rate=(deposit*interest_rate)/100; //calculate
growth_rate.
total_interest=no_years*growth_rate; //calculate
total_interest
fin_balance=total_interest+deposit; //calculate final
balance.
//display results.
cout<<endl;
cout<<"initial deposit: "<<deposit<<endl;
cout<<"interest rate:
"<<interest_rate<<endl;
cout<<"number of years: "<<no_years<<endl;
cout<<"total_interest:
"<<total_interest<<endl;
cout<<"The final balance of your account will be
"<<fin_balance<<" after "<<no_years<<"
years ";
return 0;
}
OUTPUT:
SCREENSHOT OF THE CODE:
EXPLANTATION:
when taking interest rate in percentage
final balance = deposit + total interest.