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 in simpleInterest.
  • 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.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)