This is module no.2 of our series “Programming in c” and today in this post we will learn “How to calculate simple interest in c language”. In this solution, we will see how to calculate simple interest using user-defined values. Let’s take a closer look at the below code and then try to understand what is happening in this code. In case you want this code just hover over it and click on the copy icon.
Table of Contents
ToggleProgram to find simple interest in c language
#include<stdio.h> #include<conio.h> void main() { clrscr(); int p,t; float r, si; printf("enter the values of p,r,tn"); scanf("%d %d %f", &p, &t, &r); /* now use the formula to caluclate SI SI= (p*r*t)/100 */ si= (p*r*t)/100; printf("The calculated SI is %f", si); getch(); }
So as you can see in the above code first we have declared the basic variables with the suitable datatypes. We have declared the principal, rate, time, and simple interest using the variable names p, r, t, and si as shown in line no. 7 and 8. Next, we displayed a message to the user to enter the values of his choice, to calculate simple interest, using the printf function.
Once the user entered the values for principal, rate, and time then we will assign those values to the respective variables using the scanf function.
If you notice then you will come to know that we have defined si and r variables having float datatype because there might be some chances that on calculating si the final result will have a decimal value so to avoid any chances of mistakes in our solution and to get the exact result with decimal values, we did this.
By giving si as a float datatype it will print the exact result which includes both the integer part as well as the decimal part. Look at the output screen, the final value of si has floating values in it.
So you should always keep these types of things in mind which dataype can come here and there depending upon the calculation to avoid any chance of data loss.
You can Download Turbo c++ from here and enhance your programing experience.
Turbo C++ Program Window
Output
Once you write the complete code then you need to save your program first then compile it and then finally run it. After successfully compiling and run of the program, you will see a message window where you need to enter the values of your choice as shown in the below picture. Once you entered the value then it will calculate the simple interest based on that value and then display the message along with the output.
Turbo C++ Output Window
Conclusion
If you find this program useful then feel free to share it with your friends who are facing issues in programming also your suggestion and your queries are always welcome.
Moreover, if you are having any issues with any of your programs then you can share that program with us in the comment section we will be more than happy to assist you in that case.
If you haven’t checked out our first c programming solution then no worries you can check that out by simply clicking here Sum of two numbers in C.
Also, check out the entire list of our C Programming solutions for more knowledge.
Thank you so much for reading so far.
Keep coding 🙂