Java program to calculate compound interest:
In this Java programming tutorial, we will learn how to find the compound interest with user given values. The program will use user-given values to find the compound interest.
Let’s learn how to calculate the compound interest first.
Formula to calculate compound interest:
We can use the below formula to calculate the compound interest:
Here,
- P is the principal amount.
- r is the rate of interest. It should be in decimal.
- n is the number of times annually the interest is compounded.
- t is the time.
We have to deduct the principal amount from the total value to find the interest value.
Let’s use this formula to calculate the compound interest.
Java program to calculate compound interest:
The below program uses user given values to calculate the compound interest:
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double P, r, n, t;
System.out.println("Enter the principal value: ");
P = scanner.nextDouble();
System.out.println("Enter the rate of interest: ");
r = scanner.nextDouble();
System.out.println("Enter the number of times the interest is compounded annually: ");
n = scanner.nextDouble();
System.out.println("Enter the time : ");
t = scanner.nextDouble();
double totalAmount = P * Math.pow((1 + r / (n * 100)), (n * t));
double compoundInterest = totalAmount - P;
System.out.println("Total amount: " + totalAmount);
System.out.println("Compound Interest: " + compoundInterest);
}
}
Here,
- The Scanner variable scanner is used to read the user input values.
- The double variables P, r, n and t are used to store the principal value, rate of interest, number of times the interest is compounded annually and the time in years.
- It asks the user to enter the values one by one, reads the values and assigns these to the variables.
- It calculates the total amount and assigns it to the variable totalAmount. The compound interest is calculated by subtracting the principal value from the total amount. We are using Math.pow method to calculate the power value.
If you run this program, it will print output as below:
Enter the principal value:
6000
Enter the rate of interest:
5
Enter the number of times the interest is compounded annually:
2
Enter the time :
5
Total amount: 7680.50726517814
Compound Interest: 1680.5072651781402
Enter the principal value:
6000
Enter the rate of interest:
12
Enter the number of times the interest is compounded annually:
12
Enter the time :
1
Total amount: 6760.950180791819
Compound Interest: 760.9501807918186
You might also like:
- Java program to check if a number is a composite number or not
- Java next() vs nextLine() differences with examples
- Java program to check for disjoint sets in two ways
- 4 different Java programs to convert a list to set
- Java program to convert an image to Base64 in 2 different ways
- Java program to print odd numbers from 1 to 100
- Java program to print an upper triangular matrix
- How to remove all white spaces from a file in Java