Interest calculations are super important in finance, and you can use simple formulas to compute them. This program figures out both simple and compound interest in C. For instance, if you enter a principal of 1000, a rate of 5%, and a time of 2 years, it’ll calculate both types of interest and show you the results.
Let’s see how to do this in C.
Prerequisites for the Interest Calculation Program in C
Make sure you're ready with the following:
- Understanding of
printfandscanffor input and output - Basic arithmetic operations like multiplication and division
- Use of
math.hfor power calculation in compound interest - Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler
Source Code of C Program to Calculate Simple Interest and Compound Interest
Here's the C code that calculates both simple and compound interest:
#include <stdio.h>
#include <math.h>
int main() {
float principal, rate, time, simple_interest, compound_interest;
printf("Enter principal amount: ");
scanf("%f", &principal);
printf("Enter rate of interest: ");
scanf("%f", &rate);
printf("Enter time in years: ");
scanf("%f", &time);
simple_interest = (principal * rate * time) / 100;
compound_interest = principal * pow((1 + rate / 100), time) - principal;
printf("Simple Interest = %.1f\n", simple_interest);
printf("Compound Interest = %.1f\n", compound_interest);
return 0;
}Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Output of the Interest Calculation Program
When you run the program, this is the kind of output you can expect:
Enter principal amount: 1000
Enter rate of interest: 5
Enter time in years: 2
Simple Interest = 100.0
Compound Interest = 102.5
Algorithm to Calculate Interest in C
Calculating interest requires straightforward arithmetic. For simple interest, multiply the principal, rate, and time, then divide by 100. For compound interest, use the power function to compute the terms.
- Read the principal, rate of interest, and time from the user.
- Calculate simple interest as
(principal * rate * time) / 100. - Calculate compound interest using
principal * pow((1 + rate / 100), time) - principal. - Output the calculated simple and compound interest.
flowchart TD A([Start]) --> B[/Read principal, rate, time/] B --> C[Simple Interest = (principal * rate * time) / 100] C --> D[compound\_interest = principal * (1 + rate / 100)^time - principal] D --> E[/Print simple interest and compound interest/] E --> F([End])
How the C Program to Calculate Interest Works
This program does the math to find both simple and compound interest. Let's walk through each part.
- The program starts by including the
stdio.handmath.hheaders for basic input/output and mathematical functions. - The variables
principal,rate,time,simple_interest, andcompound_interestare declared as floats to handle decimal values. - Using
printf, the program prompts the user to enter the principal amount, rate of interest, and time period, and stores them usingscanf. - Simple interest is computed as
(principal * rate * time) / 100. - Compound interest is calculated with
principal * pow((1 + rate / 100), time) - principal, wherepowcalculates the power. - Finally, it prints the calculated values of simple and compound interest with
printf. - The program ends with a
returnstatement.
Quick Quiz — test your understanding
Tap an option to check your answerWhat does the compound interest formula require?
Time and Space Complexity of the Interest Calculation Program
Analyzing the complexity helps in understanding the program's efficiency.
Time Complexity: O(1). The calculations involve a fixed number of operations: reading inputs, calculating interests, and printing results, which do not depend on the input size.
Space Complexity: O(1). The program uses a constant amount of memory to store the variables principal, rate, time, simple_interest, and compound_interest, which remain constant irrespective of input size.
Common mistake:If you forget to include math.h, you'll get errors when using pow for compound interest calculations.
Key takeaway:This program calculates both simple and compound interest efficiently with simple arithmetic in C.
FAQs About Interest Calculation in C
Here are some common questions students have about calculating interest in C.
math.h library helps with math functions like pow, which is needed for compound interest calculations.float works for reasonably large numbers, go for double for big precision.scanf grabs formatted input from the console and stores it in the variables you give. Related Programs
If you found this program helpful, check out these related programs:
- C Program to Find the Roots of a Quadratic Equation
- C Program to Calculate Difference Between Two Time Periods
- C Program to Check Leap Year with Code and Output
- C Program to Find the Area and Perimeter of a Rectangle
- C Program to Calculate Area and Circumference of Circle
Conclusion
This interest calculator’s a handy tool, showing off some basic arithmetic in C. You can expand it to cover different compounding periods or fit it into a bigger finance application. This kind of logic pops up all over in real-world finance. So dive deeper with more complex programs next!
Check out more C concepts in our C quiz or play with other examples in the C compiler.