Compound Interest Program in C language | Compound Interest Formula in C
Compound interest program in C language.
In this article, you will learn how to make a compound interest program in C language.
Example
Enter the principal (Amount), Time, and Rate::
5
7
9
Compound Interest = 9.140198
You should have knowledge of the following topics in c programming to understand this program:
- C
main()
function - C
printf()
function - C
math.h
library - C
For
loop - C Functions
Compound Interest Formula in C
CI = P * pow((1 + R / 100), T)
Where
P = Principal Amount
T = Time Amount
R = Rate Amount
CI = Compound Interest
In this article, we solved this problem in four methods:
- Using the normal calculation with the pow() function
- Using the Function
- Using the For loop
- Using the While loop
Source Code
// Compound Interest Program in C language
#include <stdio.h>
#include <math.h>
int main() {
float p, t, r, CI;
// p - To store the principle amount
// t - To store the time amount
// r - To store the rate amount
// CI - To store the compound interest value
printf("Enter the principal (Amount), Time, and Rate::\n");
scanf("%f%f%f", &p, &t, &r);
// Calculate compound interest
CI = p * (pow((1 + r / 100), t));
// It will return the final Output
printf("\nCompound Interest = %f\n", CI);
return 0;
}
Output
Enter the principal (Amount), Time, and Rate::
5
7
9
Compound Interest = 9.140198
Explanation
In this given program, we have taken three inputs Amount, Time, and Rate amount following: 5
, 7
, and 9
from the user. Then we applied the standard formula to calculate the compound interest with the pow() function.
Then this will return the 9.140198
compound interest final output of the above calculation.
Compound Interest Program in C using Function
// Compound Interest Program in C using Function
#include <stdio.h>
#include <math.h>
// This function will return the compound interest
float CalcCompound(float p, float t, float r) {
// Calculate the compound interest
return p * (pow((1 + r / 100), t));
}
// It's the driver function
int main() {
float p, t, r, CI;
// p - To store the principle amount
// t - To store the time amount
// r - To store the rate amount
printf("Enter the principal (Amount), Time, and Rate::\n");
scanf("%f%f%f", &p, &t, &r);
// It will return the final Output
printf("\nCompound Interest = %f\n", CalcCompound(p, t, r));
return 0;
}
Output
Enter the principal (Amount), Time, and Rate::
54
34
67
Compound Interest = 2017225472.000000
Compound Interest Program in C using For loop
// Compound Interest Program in C using For loop
#include <stdio.h>
int main() {
float p, t, r, CI = 0.00;
// p - To store the principle amount
// t - To store the time amount
// r - To store the rate amount
// CI - To store the compound interest value
printf("Enter the principal (Amount), Time, and Rate::\n");
scanf("%f%f%f", &p, &t, &r);
// Calculate compound interest
double TimePow = 1.00;
for (int i = 0; i < t; i++) {
TimePow *= (1 + r / 100);
if (i == (t - 1))
CI = p * TimePow;
}
// It will return the final Output
printf("\nCompound Interest = %f\n", CI);
return 0;
}
Output
Enter the principal (Amount), Time, and Rate::
56
55
34
Compound Interest = 548217024.000000
Compound Interest Program in C using While loop
// Compound Interest Program in C using While loop
#include <stdio.h>
int main() {
float p, t, r, CI = 0.00;
// p - To store the principle amount
// t - To store the time amount
// r - To store the rate amount
// CI - To store the compound interest value
printf("Enter the principal (Amount), Time, and Rate::\n");
scanf("%f%f%f", &p, &t, &r);
// Calculate compound interest
double TimePow = 1.00;
int i = 0;
while (i < t) {
TimePow *= (1 + r / 100);
if (i == (t - 1))
CI = p * TimePow;
i++;
}
// It will return the final Output
printf("\nCompound Interest = %f\n", CI);
return 0;
}
Also, visit these links
C Program to Enter Marks of Five Subjects and Calculate Percentage and Grade
C++ Program to Calculate Percentage and Grade
Java Program to Calculate Grade of students
Python Program to Calculate Total Marks Percentage and Grade of a Student