Simple Interest Calculation in C++
This document provides a C++ program to calculate simple interest. The program is well-structured and includes documentation for better understanding.
Program Explanation
The program calculates simple interest using the formula:
Simple Interest (SI) = (Principal * Rate * Time) / 100
Where:
- Principal (P): The initial amount of money.
- Rate (R): The annual interest rate (in percentage).
- Time (T): The time period in years.
C++ Program
#include <iostream>
using namespace std;
/**
* Function to calculate simple interest
* @param principal The principal amount
* @param rate The annual interest rate (in percentage)
* @param time The time period in years
* @return The calculated simple interest
*/
double calculateSimpleInterest(double principal, double rate, double time) {
return (principal * rate * time) / 100;
}
int main() {
double principal, rate, time, simpleInterest;
// Input principal, rate and time from user
cout << "Enter the principal amount: ";
cin >> principal;
cout << "Enter the annual interest rate (in percentage): ";
cin >> rate;
cout << "Enter the time period in years: ";
cin >> time;
// Calculate simple interest
simpleInterest = calculateSimpleInterest(principal, rate, time);
// Output the result
cout << "The simple interest is: " << simpleInterest << endl;
return 0;
}
Explanation of the Code
The program consists of a main function and a helper function calculateSimpleInterest
.
Function calculateSimpleInterest
This function takes three parameters:
principal
: The principal amount.rate
: The annual interest rate (in percentage).time
: The time period in years.
It calculates the simple interest using the formula mentioned above and returns the result.
Function main
The main
function performs the following steps:
- Declares variables to store the principal, rate, time, and calculated simple interest.
- Prompts the user to input the principal amount, annual interest rate, and time period.
- Calls the
calculateSimpleInterest
function with the user inputs and stores the result insimpleInterest
. - Outputs the calculated simple interest.
Conclusion
This C++ program effectively calculates simple interest based on user input. The program structure and documentation make it easy to understand and maintain.