//ITS-250, Week 5 Problem 5 //Task A: Modify this code and replace the while loo
ID: 3791321 • Letter: #
Question
//ITS-250, Week 5 Problem 5
//Task A: Modify this code and replace the while loop with a for loop
//Task B: Modify this code to allow the user the ability to enter the interest rate
#include<iostream>
using namespace std;
int main()
{
double investment;
const double INTEREST_RATE = 0.04;
int years;
int count = 1;
cout << "Enter the amount you have to invest. $ ";
cin >> investment;
cout << "Enter the number of years to invest: ";
cin >> years;
while(count <= years)
{
cout << "At the start of year " << count << ", you have $ " << investment << endl;
investment = investment + investment * INTEREST_RATE;
++count;
}
cout << "At the end of the investment period, " << " you have $" << investment << endl;
system("PAUSE");
return 0;
}
Explanation / Answer
//Task A: Modify this code and replace the while loop with a for loop
//Task B: Modify this code to allow the user the ability to enter the interest rate
#include<iostream>
using namespace std;
int main()
{
double investment;
int years;
int count = 1;
cout << "Enter the amount you have to invest. $ ";
cin >> investment;
cout << "Enter the number of years to invest: ";
cin >> years;
// TASK B:
// To allow the user the ability to enter the interest rate
cout<<"Enter the interest rate: ";
cin >> INTEREST_RATE;
// TASK A:
// To replace the while loop with a for loop
for(int count=1;count<=years;++count)
{
cout << "At the start of year " << count << ", you have $ " << investment << endl;
investment = investment + investment * INTEREST_RATE;
}
cout << "At the end of the investment period, " << " you have $" << investment << endl;
system("PAUSE");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.