1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> #include <iomanip> using namespace std; // Function to calculate simple interest double calculateSimpleInterest(double principal, double rate, double time) { return (principal * rate * time) / 100; } int main() { double principal, rate, time, interest; cout << "Simple Interest Calculator" << endl; cout << "Enter principal amount: "; cin >> principal; cout << "Enter annual interest rate (in %): "; cin >> rate; cout << "Enter time period (in years): "; cin >> time; // Calculate simple interest interest = calculateSimpleInterest(principal, rate, time); // Display the result cout << fixed << setprecision(2); // Set decimal precision to 2 places cout << "The simple interest is: $" << interest << endl; return 0; } |
Explanation
- Interest Calculation Function:
calculateSimpleInterest(double principal, double rate, double time)
: This function calculates the simple interest- Parameters:
principal
: The initial amount of money.rate
: The annual interest rate (in percentage).time
: The time period (in years).
- Return Value: Returns the calculated interest.
- Parameters:
- Main Function:
- Prompts the user to enter the principal amount, annual interest rate, and time period.
- Calls
calculateSimpleInterest()
to compute the interest. - Uses
fixed
andsetprecision(2)
from the<iomanip>
library to format the output to two decimal places. - Displays the calculated simple interest.
Notes:
- The
fixed
andsetprecision(2)
settings ensure that the output is displayed with two decimal places for better readability. - This program calculates the simple interest based on user input and provides a straightforward way to understand the concept of simple interest.