Introduction
In this lab, you will learn how to write a C program to calculate the Simple Interest for a given principal amount, rate of interest, and time duration.
Initialize Variables
Declare and initialize the variables principal_amt, rate, simple_interest, and time as float and integer types respectively, as shown below:
#include <stdio.h>
int main()
{
float principal_amt, rate, simple_interest;
int time;
}
Get User Input
Get the principal amount, rate of interest (in percentage), and time duration (in years) from the user using scanf:
printf("Enter the value of principal amount, rate and time\n\n\n");
scanf("%f%f%d", &principal_amt, &rate, &time);
Use %f format specifier to read float type input and %d to read integer type input.
Calculate Simple Interest
Calculate the simple interest using the formula:
Simple Interest = (principal_amt * rate * time) / 100
Use the below code to calculate simple interest:
simple_interest = (principal_amt*rate*time)/100.0;
Display the Output
Print the output text and variables to the console using printf function.
printf("\n\n\t\t\tAmount = Rs.%7.3f\n ", principal_amt);
printf("\n\n\t\t\tRate = Rs.%7.3f\n ", rate);
printf("\n\n\t\t\tTime = %d years \n", time);
printf("\n\n\t\t\tSimple Interest = Rs.%7.3f\n ", simple_interest);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
Use %7.3f to format the output to 7 digits including 3 digits after a decimal point.
Write the Full Code
Copy the code from Steps 1-4 into the main function in the ~/project/main.c file, as shown below:
#include <stdio.h>
int main()
{
printf("\n\n\t\tLabEx - Best place to learn\n\n\n");
float principal_amt, rate, simple_interest;
int time;
printf("Enter the value of principal amount, rate, and time\n\n\n");
scanf("%f%f%d", &principal_amt, &rate, &time);
// considering rate is in percentage
simple_interest = (principal_amt*rate*time)/100.0;
// usually used to align text in form of columns in table
printf("\n\n\t\t\tAmount = Rs.%7.3f\n ", principal_amt);
printf("\n\n\t\t\tRate = Rs.%7.3f\n ", rate);
printf("\n\n\t\t\tTime= %d years \n", time);
printf("\n\n\t\t\tSimple Interest = Rs.%7.3f\n ", simple_interest);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Compile and Run
Compile and run the program using a C compiler. The output will prompt for user input of principal amount, rate of interest, and time duration, and then display the calculated Simple Interest.
Summary
In this lab, you learned how to write a C program to calculate Simple Interest using variables, user input, and the simple interest formula. You covered the basic concepts such as declaring and initializing variables, getting user input with scanf, and displaying output using printf. Congratulations on completing the lab!



