Simple Interest Calculator in C

Program Explanation

This program calculates the simple interest based on the principal amount, the rate of interest, and the time period.
The formula used to calculate simple interest is:

    Simple Interest (SI) = (Principal * Rate * Time) / 100

The structure of the program is as follows:

  1. Include necessary headers: The #include <stdio.h> directive is used to include the standard input-output library, which is necessary for using printf and scanf functions.
  2. Function Declaration: The main function, int main(), is the entry point of the program.
  3. Variable Declaration: Declare variables to store the principal amount, rate of interest, time period, and calculated simple interest.
  4. User Input: Use scanf to take input from the user for the principal amount, rate of interest, and time period.
  5. Calculation: Compute the simple interest using the given formula.
  6. Output: Display the calculated simple interest using printf.
  7. Return Statement: The return 0; statement signifies the end of the main function.

Program Code


#include <stdio.h>

/**
 * main - Entry point of the program
 *
 * This function calculates the simple interest based on user inputs
 * for the principal amount, rate of interest, and time period.
 *
 * Return: 0 on success
 */
int main() {
    // Variable declaration
    float principal, rate, time, simpleInterest;

    // Taking user input
    printf("Enter the principal amount: ");
    scanf("%f", &principal);

    printf("Enter the rate of interest: ");
    scanf("%f", &rate);

    printf("Enter the time period in years: ");
    scanf("%f", &time);

    // Calculating simple interest
    simpleInterest = (principal * rate * time) / 100;

    // Displaying the result
    printf("The simple interest is: %.2f\n", simpleInterest);

    // Return statement
    return 0;
}

Documentation

The program includes documentation comments to explain its structure and functionality:

  • Header Inclusion:
    #include <stdio.h>

    This line includes the standard input-output library necessary for input and output operations.

  • Main Function:
    
    /**
     * main - Entry point of the program
     *
     * This function calculates the simple interest based on user inputs
     * for the principal amount, rate of interest, and time period.
     *
     * Return: 0 on success
     */
            

    The main function is the entry point of the program where execution starts.

  • Variable Declaration:
    float principal, rate, time, simpleInterest;

    Declares four variables to store the principal amount, rate of interest, time period, and calculated simple interest.

  • User Input:
    
    printf("Enter the principal amount: ");
    scanf("%f", &principal);
    
    printf("Enter the rate of interest: ");
    scanf("%f", &rate);
    
    printf("Enter the time period in years: ");
    scanf("%f", &time);
            

    Prompts the user to enter the principal amount, rate of interest, and time period. The inputs are stored in the respective variables.

  • Calculation:
    simpleInterest = (principal * rate * time) / 100;

    Calculates the simple interest using the provided formula.

  • Output:
    printf("The simple interest is: %.2f\n", simpleInterest);

    Displays the calculated simple interest.

  • Return Statement:
    return 0;

    Indicates the end of the main function and returns 0 to the operating system.

 

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 :)