Published: December 6, 2024 | Reading Time: 5 minutes
C is a versatile programming language known for its robust capacity to write everything from scratch. It has an extensive library of built-in functions, data types, and operators. Hence, it enables developers to create everything from basic to highly complex programs. Solving problems like calculating simple interest is a very good way to demonstrate the practical use of C. It also helps strengthen foundational coding skills essential for any programmer. In this article, we will learn all about simple interest program in C and how to write good code for it.
The amount charged for a financial tool called a 'loan' besides its principal amount within a certain period is known as simple interest. It is a calculation in mathematics and in finance when money is involved, both borrowing and lending.
In loans whenever a person gets a loan from a particular lender, the initial amount which he or she gets is called principal. They must pay off the principal over time and, in addition, a certain amount, which is referred to as simple interest. This interest is obtained from the principal, the time horizon of the loan and the contracted rate of interest. In other words, simple interest is also known as the price that is paid for borrowing other people's money.
Simple Interest (SI) = (Principal × Time × Rate of Interest) / 100
Where:
The algorithm for a simple interest program has the following structure:
Now, let's take a look at the program to calculate simple interest in C. There are many ways to write the code. Here, we'll look at two examples:
In this program, simple interest is calculated using a separate function. The function simple_interest takes three parameters: principal (P), rate of interest (R), and time (T). It computes the simple interest using the formula SI = (P×R×T)/100 and returns the result.
In the main function you have to input values for P, R, and T. These are passed to the simple_interest function. The returned value is stored in SI and displayed.
#include <stdio.h>
#include <stdlib.h>
double simple_interest(int P, int R, int T) {
return (P * R * T) / 100.0; // Ensure division by 100.0 for correct float result
}
int main() {
int P, R, T;
printf("Enter the principal amount\n");
scanf("%d", &P);
printf("Enter the rate of interest\n");
scanf("%d", &R);
printf("Enter the time\n");
scanf("%d", &T);
double SI = simple_interest(P, R, T);
printf("The Simple Interest = %.2lf\n", SI); // Use %.2lf for 2 decimal places
return 0;
}
double data type for accurate decimal results%.2lfThis is a program to calculate simple interest using a while loop. You have to input three values: principal amount, time, and rate of interest. A variable x is initialised to 1 and iteratively multiplied by each input value (y) within the loop. Once all three inputs are processed, the simple interest is computed using the formula SI = (x/100). Here x already holds the product of the inputs. Finally, the calculated simple interest is displayed.
// C Program to Calculate Simple Interest using While loop
#include <stdio.h>
int main() {
float x = 1, y, SI;
// `SI` = value of the simple interest
printf("Enter the principal (amount), time, and rate::\n");
int count = 1;
while (count <= 3) {
scanf("%f", &y);
// It will calculate the value of simple interest
x *= y;
if (count == 3)
SI = x/100;
count++;
}
// It will produce the final output
printf("\nSimple Interest = %.2f\n", SI);
return 0;
}
Understanding how to calculate simple interest from the point of programming is a fundamental skill for software developers. It helps you grasp essential concepts of financial computations and apply logic to solve real-world problems. This program is a simple example of basic programming that introduces developers to using variables, formulas, and input-output functions in C. You can further practice writing code for simple interest programs in C using different loops, pointers and functions. It's important to practice different ways to write code and become proficient at it.
If you are looking forward to a successful career in software development, you will have to build a strong foundation and work toward it throughout your college years. Enroll into the CCBP 4.0 Academy to get started on your journey as a software professional.
To calculate simple interest in C, you need to input the principal, rate, and time, then use the formula SI = (P×R×T) / 100. The program should output the calculated interest.
Using functions to write program to find simple interest makes the code modular and reusable. It helps separate the logic of calculating interest from the rest of the program. Hence it improves readability and allows you to call the function multiple times.
In C, float or double data types are used to store decimal values for the principal, rate, and time. Using these is necessary so fractional interest rates are accurate.
Yes, you can use a while loop to repeatedly take inputs like principal, rate, and time. Then, use it to calculate the simple interest.
You can use input validation techniques like if conditions or while loops to ensure that the user enters valid numeric values.
NxtWave provides comprehensive programming education through CCBP 4.0 Academy and other learning programs. For more information about courses and career guidance, visit www.ccbp.in.