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.

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

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.

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

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.

`/*`

`* C Program to find compound interest using formula`

`*/`

`#include <stdio.h>`

`#include <math.h>`

int main()

`{`

double Principal;

printf("Enter the Principal Amount: ");

scanf("%lf", &Principal);

double Rate;

printf("Enter the Interest Rate: ");

scanf("%lf", &Rate);

double Time;

printf("Enter the Time Period(in Years): ");

scanf("%lf", &Time);

`// Calculating compound interest`

double Amount;

Amount = Principal * pow((1 + Rate / 100), Time);

double Compound_Interest;

Compound_Interest = Amount - Principal;

printf("The Compound Interest is %.2lf ",Compound_Interest);

return 0;

`}`

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.

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

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

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.

`/*`

`* C Program to calculate compound interest using function`

`*/`

`#include <stdio.h>`

`#include <math.h>`

void Compound_Interest(double Principal, double Rate, double Time)

`{`

`// Calculating compound interest`

double Amount;

Amount = Principal * pow((1 + Rate / 100), Time);

double Compound_Interest;

Compound_Interest = Amount - Principal;

printf("The Compound Interest is %.2lf ", Compound_Interest);

`}`

int main()

`{`

double Principal;

printf("Enter the Principal Amount: ");

scanf("%lf", &Principal);

double Rate;

printf("Enter the Interest Rate: ");

scanf("%lf", &Rate);

double Time;

printf("Enter the Time Period(in Years): ");

scanf("%lf", &Time);

Compound_Interest(Principal, Rate, Time);

return 0;

`}`

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.

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”.

**Next Steps:**

- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**Related Posts:**

- Buy C Books
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Apply for C Internship
- Buy Computer Science Books