In: Computer Science
C++ programming interest using for loops.
I'm fairly new to C++ programming. I really don't get for loops, and wanted help with this question. How do I go about it?
1a.
Write a program to ask the user for the starting balance of their savings account, what interest rate they are earning, and how many years they are planning to keep the account open. To calculate the new balance, including compounded interest: use a for loop to loop through the number of years the user specifies (1, 2, 3, 4, … years) and each time through the loop update the account balance using the following formula:
balance = balance * (1 + interest rate / 100.0) //
this adds one year’s interest to the account
balance = initialBalance * (1 + interest rate / 100.0) // this will
only calculate one year’s interest no matter how
many times you repeat it.
1b.
Now write the code to show the user how much money they would have after ONE year, depending on the different interest rates. Ask the user for a minimum and maximum interest rate (e.g. 3.0 and 8.5 … note you may need to swap the values if the user enters them out of order). Use a for loop to loop through the possible interest rates at 0.5 increments (3.0, 3.5, 4.0, 4.5, … 8.5) and each time through the loop calculate the user’s account balance after one year at the specified interest rate:
balance = initialBalance * (1 + interest rate / 100.0) // this time you want the initial balance
Thanks for the question.
Here is the completed code for this problem.
are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change.
If you are satisfied with the solution, please rate the answer.
Thanks
===========================================================================
#include<iostream>
using namespace std;
int main(){
double balance=0.0, interest_rate=0.0;
int years;
cout<<"Enter your starting balance: $";
cin>>balance;
cout<<"Enter interest rate (in %): ";
cin>>interest_rate;
cout<<"Enter the number of years planning to
keep your account open: ";
cin>>years;
// starting from year =1 upto years
for(int year=1; year<=years; year++){
// the below line will execute that
many times
// as the number of years
given;
// so if user enter 5 years, then
the below line will execute
// 5 times
balance =
balance*(1+interest_rate/100);
// after all the above lines are
executed
// year will be incremented by 1 ;
ie. year++
}
cout<<"\nAfter "<<years<<" your
total balance will be $"<<balance<<endl;
}
===========================================================================
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
double min_rate, max_rate;
double intialBalance;
cout<<"Enter initial balance: ";
cin>>intialBalance;
cout<<"Enter minimum interest rate (%): ";
cin>>min_rate;
cout<<"Enter maximum interest rate (%): ";
cin>>max_rate;
// you may need to swap the values if the user enters
them out of order).
if(max_rate<min_rate){
cout<<"Swapping the max and
min as they are out of order\n\n";
double temp_Rate = max_rate;
max_rate=min_rate;
min_rate=temp_Rate;
}
double temp_balance;
cout<<fixed<<showpoint<<setprecision(2);
for (double rate=min_rate; rate<=max_rate;
rate=rate+0.5){
temp_balance = intialBalance;
temp_balance =
temp_balance*(1+rate/100.0);
cout<<"At interest Rate:
"<<rate<<"%, after 1 year you will have
$"<<temp_balance<<endl;
}
}
thanks a lot : )