Compound Interest
Compound interest (or compounding interest) is the interest calculated on the principal, including all cumulative interest on deposits or loans in the previous period. – Investopedia
Logic
To calculate compound interest we have to use a very simple formula shown below:
Compound interest formula
Where, P = Principle T = Time and R = Rate
To calculate compound interest we have to multiply principle amount with the power of (1 + Rate / 100) with time as an exponent. To calculate power we have used pow() method of the class Math.
Program
import java.util.Scanner; public class CompoundInterest { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter principal : "); float principal = scanner.nextFloat(); System.out.println("Enter rate : "); float rate = scanner.nextFloat(); System.out.println("Enter time : "); float time = scanner.nextFloat(); // Calculate compound interest double result = principal * Math.pow((1 + rate / 100), time); System.out.println("Compound interest : " + result); } }
Output
Enter principal : 100
Enter rate : 25
Enter time (year) : 6
Compound interest : 381.4697265625