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:
- Include necessary headers: The
#include <stdio.h>
directive is used to include the standard input-output library, which is necessary for usingprintf
andscanf
functions. - Function Declaration: The main function,
int main()
, is the entry point of the program. - Variable Declaration: Declare variables to store the principal amount, rate of interest, time period, and calculated simple interest.
- User Input: Use
scanf
to take input from the user for the principal amount, rate of interest, and time period. - Calculation: Compute the simple interest using the given formula.
- Output: Display the calculated simple interest using
printf
. - 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.