Calculate Simple Interest in C++
Simple interest in C++: P × R × T / 100. Rate 5 means 5%, not 0.05.
1#include <iostream>2#include <iomanip>3using namespace std;45int main() {6 float principal, rate, time, interest;78 cout << "Enter principal amount: ";9 cin >> principal;1011 cout << "Enter rate of interest (per year): ";12 cin >> rate;1314 cout << "Enter time (in years): ";15 cin >> time;1617 // Simple Interest = (P * R * T) / 10018 interest = (principal * rate * time) / 100;1920 cout << fixed << setprecision(2);21 cout << "Simple Interest = " << interest << endl;22 cout << "Total Amount = " << principal + interest << endl;2324 return 0;25}
Output
Enter principal amount: 10000 Enter rate of interest (per year): 5 Enter time (in years): 2 Simple Interest = 1000.00 Total Amount = 11000.00
What's going on
Simple interest in C++ is P * R * T / 100. Rate 5 means five percent, not 0.05.
float principal, rate, time, interest;
float (or double) so 10000 * 5 * 2 / 100 keeps the tenths. The same formula in int truncates.
cin >> principal; then rate, then time.
Sample: 10000, 5, 2.
interest = (principal * rate * time) / 100;
10000 * 5 * 2 / 100 = 1000. That's the sample. Total amount is principal + interest = 11000.
Enter 0.05 for the rate and the result is 100× too small (10 instead of 1000). The prompt says "per year" as a percent.
cout << fixed << setprecision(2);
Two digits after the dot, same as the float-multiplication program. Needs <iomanip>.
Sanity check: 1000 at 5% for 2 years is 100. If that isn't what you get, the rate interpretation is wrong.
This is not compound interest. Compound uses a power — that's the next program.