Compound Interest
Completed
Read principal, yearly rate (%) and years. Print the amount after each year with 2 decimals, one per line.
Input
Three numbers: P r years.
Output
One line per year.
Example
Input
1000 5 3
Output
year 1: 1050.00 year 2: 1102.50 year 3: 1157.62
Editor
C++
Hints
Multiply by (1 + r/100) once per year.
Solution explanation
Updating a variable in a loop models growth step by step.
Show solution
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double p, r;
int years;
cin >> p >> r >> years;
cout << fixed << setprecision(2);
for (int y = 1; y <= years; y++) {
p *= 1 + r / 100;
cout << "year " << y << ": " << p << "\n";
}
return 0;
}