In this post, we will learn how to calculate compound interest using Java Programming language. Let’s understand Compound Interest and “how to calculate Compound Interest” so that we can code the Java Program to Calculate Compound Interest.
What is Compound Interest ?
Compound Interest is an interest paid on both the Principal and the interest compounded at regular intervals.
Compound Interest Formula
The General Formula for Compound interest is as follows:
CI = P * ( 1 + r/n )nt - P
where CI = Compound Interest
P = Principal Amount
R = Rate of Interest in Percentage
T = Time Period
n = number of times the interest is compounded annually
But, commonly we take compounding annually then the formula becomes:
CI = P * ( 1 + r/100 )t - P
Note : Amount = P * ( 1 + r/100 )t
Hence, Compound Interest is also calculated as, CI = Amount – Principle
Now, let’s see the Java Program to Calculate Compound Interest using the formula.
Java Program to Calculate Compound Interest
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter the Principal : "); double P = sc.nextDouble(); System.out.println("Enter the Rate : "); double R = sc.nextDouble(); System.out.println("Enter the Time : "); double T = sc.nextDouble(); //Calculating Amount double A = P * Math.pow(( 1 + R/100 ),T); //Calculating the Compount Interest double CI = A - P; //displaying the Result System.out.printf("The Compound Interest is %.2f",CI); } }
Output
Enter the Principal : 10000
Enter the Rate : 10
Enter the Time : 5
The Compound Interest is 6105.10
How Does This Program Work ?
Scanner sc = new Scanner(System.in); System.out.println("Enter the Principal : "); double P = sc.nextDouble(); System.out.println("Enter the Rate : "); double R = sc.nextDouble(); System.out.println("Enter the Time : "); double T = sc.nextDouble();
In this program, we first take the input of Principle, Rate of Interest, Time Period and stored them into variables P, R, T of double datatype respectively. The input is taken by using Scanner Class in Java.
//Calculating Amount double A = P * Math.pow(( 1 + R/100 ),T); //Calculating the Compount Interest double CI = A - P;
Then, we calculated the amount using the formula, A = P (1+R/100)t and then we calculated the compound interest using amount and the formula, CI = A – P
//displaying the Result System.out.printf("The Compound Interest is %.2f",CI);
Then we have displayed the result using the System.out.printf("%.2f",CI)
, to print the CI to two decimal places.
This is the Java Program to Calculate Compound Interest.
Read: Java Program to Calculate Simple Interest
Conclusion
I hope after going through this post, you understand how to code Java Program to Calculate Compound Interest.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.
Also Read: