
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Compound Interest in Java
In this article, we will understand how to calculate the compound interest in Java. Compound interest is calculated using the following formula ?
Principle*(1+(rate / 100))^time - Principle
Compound Interest ? The percentage interest charged on principal and accrued interest. Rates are higher compared to simple interest.
We'll explore how to get user input for the principal amount, interest rate, and time period, and then calculate the compound interest based on the provided formula. By the end, you'll understand how to implement this formula in Java for both user-defined and predefined values.
Problem Statement
Write a Java program to calculate the compound interest using the formula. Below is a demonstration of the same ?
Input
Enter a Principle number : 100000 Enter Interest rate : 5 Enter a Time period in years : 3
Output
The Compound Interest is : 15762.50000000001
Different Approaches
Below are the different approaches to calculating the compound interest ?
For User Defined Input
Below are the steps to calculate the compound interest by using user-defined input ?
- Import the required package by using the Scanner class from java.util package.
- Define a scanner object which is used to capture user input.
- Prompt user for input to get the principal amount, interest rate, and time period.
- Calculate compound interest by using the formula to calculate based on user input.
- Display the calculated compound interest.
Example
Here, the input is being entered by the user based on a prompt. You can try this example live in our coding ground tool .
import java.util.Scanner; public class CompoundInterest { public static void main (String args[]){ double principle, rate, time, compound_interest; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A Scanner object has been defined "); System.out.print("Enter a Principle number : "); principle = my_scanner.nextInt(); System.out.print("Enter Interest rate : "); rate = my_scanner.nextInt(); System.out.print("Enter a Time period in years : "); time = my_scanner.nextInt(); compound_interest = principle * (Math.pow((1 + rate / 100), time)) - principle; System.out.println("The Compound Interest is : " + compound_interest); } }
Output
Required packages have been imported A Scanner object has been defined Enter a Principle number : 100000 Enter Interest rate : 5 Enter a Time period in years : 3 The Compound Interest is : 15762.500000000015
For Predefined Value:
Below are the steps to calculate the compound interest by using the predefined value ?
- Initialize variables by redefining the values for principal, rate, and time.
- Display the predefined principal amount, interest rate, and time period.
- Calculate compound interest by using the predefined values in the formula.
- Display the calculated compound interest.
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class CompoundInterest{ public static void main (String args[]){ double principle, rate, time, compound_interest; principle = 100000; rate = 5; time = 3; System.out.printf("The Principle amount is %f \nThe interest rate is %f \nThe time period in years is %f " , principle, rate, time); compound_interest = principle * (Math.pow((1 + rate / 100), time)) - principle; System.out.println("\nThe Compound Interest is: " + compound_interest); } }
Output
The Principle amount is 100000.000000 The interest rate is 5.000000 The time period in years is 3.000000 The Compound Interest is: 15762.50000000001