# C Program to Find Compound Interest

«
»
Problem Description:

Write a C program to calculate compound interest.

What is Compund Interest?
Compound Interest is the interest on principal as well as the interest earned over the period. That is for the first year, it will return interest on principal, but from the second year it’s new principal will be the original principal plus the interest upto previous year. Principal refers to the original sum of money invested.

Compound Interest Formula:

Compound Interest is calculated using the formula:

$$A = P × \Bigl(1 + \frac{R}{100}\Bigl)^T$$

Here,
A is The Amount.
P is the Principal Amount.
R is the Interest Rate.
T is the total Elapsed Time Period.

So, the Compound interest is the difference between Amount (A) and Principal (P) that is Compound Interest (CI) = A-P

Example:

Let’s say the question is like this –
Find the Compound Interest on Rs. 5000 for 2 years at 10% per annum compounded annually.

Solution: Given, P = 5000, R = 10 and T = 2.
$$A = P × \Bigl(1 + \frac{R}{100}\Bigl)^T$$
$$A = 5000 × \Bigl(1 + \frac{10}{100}\Bigl)^2$$
A = 5000×1.1×1.1
A = 6050

Now, CI = A – P = 6050 – 5000 = 1050.

Participate in C Programming Certification Contest of the Month Now!

Hence, the Compound Interest on Rs.5000 at the rate of 10% for 2 years is Rs.1050.

Compound Interest Algorithm:

1. Start
2. Declare the principal, Rate, and Time Variable
3. Take the user input for each of them
4. Calculate the Compound interest using the formula
CI = P×(1+R/100)^T – P
5. Print the compound interest
6. End
Problem Solution

There are several ways to calculate the compound interest in C language. Let’s take a detailed look at all the approaches to find the compound interest in C.

Method 1: Compound Interest Program in C Using Naive Approach

In this approach, we are calculating the value of compound interest using the formula.

Program/Source Code

Here is the source code of the C Program to find compound interest using formula. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

1. /*
2.  * C Program to find compound interest using formula
3.  */
4. #include <stdio.h>
5. #include <math.h>
6. 
7. int main()
8. {
9.     double Principal;
10.     printf("Enter the Principal Amount: ");
11.     scanf("%lf", &Principal);
12. 
13.     double Rate;
14.     printf("Enter the Interest Rate: ");
15.     scanf("%lf", &Rate);
16. 
17.     double Time;
18.     printf("Enter the Time Period(in Years): ");
19.     scanf("%lf", &Time);
20. 
21.     // Calculating compound interest
22.     double Amount;
23.     Amount = Principal * pow((1 + Rate / 100), Time);
24. 
25.     double Compound_Interest;
26.     Compound_Interest = Amount - Principal;
27.     printf("The Compound Interest is %.2lf ",Compound_Interest);
28. 
29.     return 0;
30. }
Program Explanation

1. Declare Variable named Principal, Rate, Time, Amount, and Compound Interest.
2. Take the user input for Principal, Rate, and Time using scanf() function of C and Computed the Amount using Formula –
$$A = P × \Bigl(1 + \frac{R}{100}\Bigl)^T$$
And store the value in Amount Variable
3. Further, calculate the Compound Interest using the formula:
CI = A – P
And store the value in a Variable named Compound_Interest.
4. Finally, print the Compound Interest using the printf() function of C.

Note:

• Declare all variables as double because principal, rate, and time could be in decimal/fractional in nature say Rs. 5000.50 or rate 9.57% or 2.5 Years.
• Also, the range of double is more than float, so the program will execute properly for larger values of input and output.
• %lf format specifier is used to take/print double values.
• Pow() is a special function in C, and it is Present in math.h header file.
• Pow(x,y) equals x^y

Time Complexity: O(1)
The time complexity is O(1) because the program runs in constant time.

Space Complexity: O(1)
The space complexity is O(1) because the program does not require any extra spaces.

Run Time Testcases

In this case, enter the values ​​for calculating compound interest are: P=5000, R=10, T=2.

Enter the Principal Amount: 5000
Enter the Interest Rate: 10
Enter the Time Period(in Years): 2
The Compound Interest is 1050.00

Method 2: Compound Interest Program in C Using Function

In this approach, we are calculating the value of compound interest using function.

Program/Source Code

Here is the source code of the C Program to find compound interest using function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

1. /*
2.  * C Program to calculate compound interest using function
3.  */
4. 
5. #include <stdio.h>
6. #include <math.h>
7. 
8. void Compound_Interest(double Principal, double Rate, double Time)
9. {
10.     // Calculating compound interest
11.     double Amount;
12.     Amount = Principal * pow((1 + Rate / 100), Time);
13. 
14.     double Compound_Interest;
15.     Compound_Interest = Amount - Principal;
16. 
17.     printf("The Compound Interest is %.2lf ", Compound_Interest);
18. }
19. 
20. int main()
21. {
22.     double Principal;
23.     printf("Enter the Principal Amount: ");
24.     scanf("%lf", &Principal);
25. 
26.     double Rate;
27.     printf("Enter the Interest Rate: ");
28.     scanf("%lf", &Rate);
29. 
30.     double Time;
31.     printf("Enter the Time Period(in Years): ");
32.     scanf("%lf", &Time);
33. 
34.     Compound_Interest(Principal, Rate, Time);
35. 
36.     return 0;
37. }
Program Explanation

In driver program, declare the variable named Principal, Rate, and Time and took the user input for each of them and called the Compound_Interest function by passing the above created variables as arguments.

In Compound_Interest function passed the principal, Rate, and Time as Argument and Computed the Compound interest by the formula –
$$CI = P × \Bigl(1 + \frac{R}{100}\Bigl)^T – P$$

Note:

• Void function means the function does not return a value.
• Function enhances the code reusability.

Time Complexity: O(1)
The time complexity is O(1) because the program runs in constant time.

Space Complexity: O(1)
The space complexity is O(1) because the program does not require any extra spaces.

Run Time Testcases

In this case, the values used to calculate compound interest are: P=10000, R=5, T=4.

Enter the Principal Amount: 10000
Enter the Interest Rate: 5
Enter the Time Period(in Years): 4
The Compound Interest is 2155.06

Applications:

• Compound Interest is frequently used to calculate growth and decay, inflation, increase and decrease problems.
• It is one of the important mathematical tools for banking and finance industry.

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”. 