Example 1: Calculate Simple Interest in Java
Output
Enter the principal: 1000 Enter the rate: 8 Enter the time: 2 Principal: 1000.0 Interest Rate: 8.0 Time Duration: 2.0 Simple Interest: 160.0
In the above example, we have used the Scanner class to take principal, rate, and time as input from the user. We then use the formula of simple interest to compute the simple interest.
Simple Interest = (Principal * Rate * Time) / 100
Example 2: Calculate Compound Interest
Output
Enter the principal: 1000 Enter the rate: 10 Enter the time: 3 Enter number of times interest is compounded: 1 Principal: 1000.0 Interest Rate: 10.0 Time Duration: 3.0 Number of Time interest Compounded: 1 Compound Interest: 331.00000000000045
In the above example, we have used the formula of compound interest to calculate the compound interest.
Here, we have used the Math.pow() method to calculate the power of the number.