C Program to Calculate Simple Interest
Calculating simple interest is one of the most common ways to practice basic arithmetic operators like multiplication and division in C.
The Formula
To calculate the interest and the final maturity amount, we use these equations:
- **Simple Interest (SI)** = (Principal × Rate × Time) / 100
- **Total Amount** = Principal + Simple Interest
Program Implementation
C
#include <stdio.h>
int main() {
float principalAmount, rateOfInterest, timePeriod;
float simpleInterest, totalBalance;
printf("Enter the Principal amount: ");
scanf("%f", &principalAmount);
printf("Enter the Annual Interest Rate (%%): ");
scanf("%f", &rateOfInterest);
printf("Enter the Time (in years): ");
scanf("%f", &timePeriod);
// Calculating Interest: (P * R * T) / 100
simpleInterest = (principalAmount * rateOfInterest * timePeriod) / 100.0;
// Calculating Total Maturity Amount
totalBalance = principalAmount + simpleInterest;
printf("\n--- Calculation Results ---\n");
printf("Interest Earned: %.2f\n", simpleInterest);
printf("Total Balance: %.2f\n", totalBalance);
return 0;
}
Sample Output
Enter the Principal amount: 100 Enter the Annual Interest Rate (%): 30 Enter the Time (in years): 2 --- Calculation Results --- Interest Earned: 60.00 Total Balance: 160.00
Explanation
- **Variable Naming**: We used descriptive names like `principalAmount` and `timePeriod` to make the code self-documenting.
- **Input/Output**: The `%f` format specifier is used for `float` variables to allow decimal values.
- **Calculation**: We divide by `100.0` to ensure the division is handled as a floating-point operation, which prevents rounding errors common in integer division.
- **Formatting**: `%.2f` is used in the final print statement to display the currency values with exactly two decimal places.
Note: Pro Tip: If you need to handle very large financial figures with extreme precision, consider using the 'double' data type instead of 'float'.
Codecrown