Program to Calculate Compound Interest [C, C++, Python & Java]

Posted in

Program to Calculate Compound Interest [C, C++, Python & Java]
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    In this tutorial, we will learn how to write a program in C, C++, Python, and Java to calculate the compound interest.

    To write the program, you simply need to know the syntax of a programming language and the compound interest formula. Before we move to write a program, let us understand what compound interest is.

    What is Compound Interest?

    In simple terms, compound interest is the interest of interest. It is the interest imposed on the principal amount or a loan or deposit amount. It depends entirely on the principal amount and the interest gained over time. Conversely, in simple interest, the interest amount is not added to the principal amount while computing the interest for the next time period.

    We generally denote compound interest as CI.

    The formula to calculate compound interest is:

    Amount= P(1 + R/100)t

    Compound Interest (CI) = Amount – P

    where,

    P - principal amount

    R - rate of interest

    t - time period

    C Program to Calculate Compound Interest

    #include<stdio.h>
    #include<math.h>
    int main()
    {
        float CI,p,r,t;
        printf("Enter the Principal value: ");
        scanf("%f", &p);
        printf("Enter the rate value: ");
        scanf("%f", &r);
        printf("Enter the timespan: ");
        scanf("%f", &t);
        CI = p*pow((1+(r/100)),t);
        printf("The compound interest is: %f", CI);
        return 0;
    }

    Output

    Enter the Principal value: 30000
    Enter the rate value: 4
    Enter the timespan: 8 
    The compound interest is: 41057.058594

    C++ Program to Calculate Compound Interest

    #include<iostream>
    #include<cmath>
    using namespace std;
    int main()
    {
        float CI,p,r,t;
        cout<<"Enter the Principal value: ";
        cin>>p;
        cout<<"Enter the rate of interest: ";
        cin>>r;
        cout<<"Enter the timespan: ";
        cin>>t;
        CI = p*pow((1+(r/100)),t);
        cout<<"The compound interest is: "<<CI;
        return 0;
    }

    Output

    Enter the Principal value: 30000
    Enter the rate of interest: 20
    Enter the timespan: 10
    The compound interest is: 185752.0926719999

    Python Program to Calculate Compound Interest

    p = float(input("Enter the Principal value: "))
    r= float(input("Enter the rate of interest: "))
    t= float(input("Enter the timespan: "))
    CI =p*((1+(r/100))**t)
    print("The compound interest is:",CI)

    Output

    Enter the Principal value: 20000
    Enter the rate of interest: 3
    Enter the timespan: 4
    The compound interest is: 22510.1762

    Java Program to Calculate Compound Interest

    import java.util.*;
    import java.lang.*;
    public class Main 
    {
        public static void main(String[] agrs)
        {
           double CI,p,r,t;
           Scanner sc = new Scanner(System.in);
           System.out.println("Enter the Principal value: ");
           p = sc.nextDouble(); 
           System.out.println("Enter the rate of interest: ");
           r = sc.nextDouble(); 
           System.out.println("Enter the timespan: ");
           t = sc.nextDouble(); 
           CI = p * Math.pow((1 + r / 100), t);
           System.out.println("The compound interest is: " +CI);
        }
    }

    Output

    Enter the Principal value: 
    40000
    Enter the rate of interest: 
    7
    Enter the timespan: 
    8
    The compound interest is: 68727.44719327684

    Conclusion

    Wasn't it easy to implement the program to calculate compound interest? In the above programs, we used the pow() method to calculate the power of a number, which simplified our task. Try to implement the same on your system and understand the program's working.

    Good luck!

    People are also reading:

    Leave a Comment on this Post

    0 Comments