C Program to Calculate Compound Interest

Howdy readers, today you will learn how to write a program to calculate compound interest using C Programming language.

C Program to Calculate Compound Interest

Compound interest is the interest on a loan or deposit calculated based on both the initial principal and the accumulated interest from previous periods.

The formula to calculate compound interest is:

CI = {p (1 + r/100)^t} - p

Where, 
p = Principal Amount
r = Rate of Interest
t = Time Period

The below program prompts the user to enter the principal amount, rate of interest and time period. Then, we used the above formula to compute the compound interest.

So, without any delay, let’s begin this tutorial.

C Program to Calculate Compound Interest

C Program

// C Program to Calculate Compound Interest
#include <stdio.h>
#include <math.h>

int main(){
    float p, r, t, CI;
    
    // Asking for input
    printf("Enter the principal amount: ");
    scanf("%f", &p);
    printf("Enter the rate of interest: ");
    scanf("%f", &r);
    printf("Enter the time period: ");
    scanf("%f", &t);
    
    // Calculating compound interest
    CI = p * pow((1 + r/100), t) - p;
    
    // Displaying result
    printf("The compound interest is: %.2f", CI);
    
    return 0;
}

Output

Enter the principal amount: 30000
Enter the rate of interest: 3
Enter the time period: 6
The compound interest is: 5821.56
C Program to Calculate Compound Interest

Explanation

    float p, r, t, CI;

We have declared four float data type variables named p, r, t, and CI.

    // Asking for input
    printf("Enter the principal amount: ");
    scanf("%f", &p);
    printf("Enter the rate of interest: ");
    scanf("%f", &r);
    printf("Enter the time period: ");
    scanf("%f", &t);

The program prompts the user to enter the principal amount, rate of interest and time period. These values get stored in the p, r, and t named variables respectively.

    // Calculating compound interest
    CI = p * pow((1 + r/100), t) - p;

The compound interest is computed using the formula,

  • Compound Interest = [principal * (1 + rate)time] – principal 

The compound interest calculated above gets stored in the CI named variable.

    // Displaying result
    printf("The compound interest is: %.2f", CI);

Finally, the compound interest is printed on the screen using printf() function. The %.2f format specifier is used to limit the result to 2 decimal places.

Conclusion

I hope after reading this tutorial, you get to know how to write a program to calculate compound interest using C Programming language.

If you have any queries regarding the program, then let us know in the comment section. We will be pleased to solve all of your queries.

Leave a Comment