In this tutorial, you will learn how to write a Java program to calculate compound interest. First, we will understand the formula for compound interest.
Next, we will write a basic program by assigning values directly to variables. Finally, we will build an interactive program using the user-defined method and Scanner class to accept the principal amount, rate of interest, and time from the user.
Understanding the Compound Interest Formula
Compound interest is calculated on the principal amount plus the interest accumulated during previous periods. The formula to calculate the total amount (A) with compound interest is:
A = P × (1 + R/100)^T
Where:
- P = Principal amount
- R = Annual rate of interest
- T = Time period in years
- A = Final amount
And the compound interest (CI) is:
CI = A − P
Therefore:
CI = P × (1 + R/100)^T − P
For example, suppose the principal amount is ₹10,000, the annual interest rate is 5%, and the time period is 2 years.
Applying these values to the formula, the total amount is calculated by multiplying 10,000 by 1 plus 5 divided by 100 squared. This simplifies to 10,000 multiplied by 1.1025, resulting in a total amount of ₹11,025. Finally, subtracting the initial principal from this total gives a compound interest of ₹1,025.
Given: - Principal (P) = ₹10,000 - Rate (R) = 5% per year - Time (t) = 2 years Formula: A = P * (1 + R / 100)^t Compound Interest = A - P Step-by-Step Calculation: A = 10,000 * (1 + 5 / 100)^2 A = 10,000 * (1 + 0.05)^2 A = 10,000 * (1.05)^2 A = 10,000 * 1.1025 A = ₹11,025 Compound Interest = 11,025 − 10,000 = ₹1,025
Java Program to Calculate Compound Interest with Pre-assigned Values
Problem Statement: Write a Java program to calculate compound interest by assigning values directly to variables, such as principal, rate, and time.
Java Program
public class CompoundInterestPreassigned {
public static void main(String[] args) {
// Pre-assigned values
double principal = 10000.0;
double rate = 5.0;
double time = 2.0;
// Calculating the total amount
double amount = principal * Math.pow(1 + (rate / 100), time);
// Calculating the compound interest
double compoundInterest = amount - principal;
// Displaying the results
System.out.println("--- Compound Interest Calculation ---");
System.out.println("Principal: ₹" + principal);
System.out.println("Annual Rate: " + rate + "%");
System.out.println("Time Period: " + time + " years");
System.out.println("-------------------------------------");
System.out.printf("Total Amount (A): ₹%.2f\n", amount);
System.out.printf("Compound Interest (CI): ₹%.2f\n", compoundInterest);
}
}Expected Output:
--- Compound Interest Calculation --- Principal: ₹10000.0 Annual Rate: 5.0% Time Period: 2.0 years ------------------------------------- Total Amount (A): ₹11025.00 Compound Interest (CI): ₹1025.00
In this example program:
- The principal variable stores the initial amount.
- The rate variable stores the annual interest rate.
- The time variable stores the duration in years.
- The Math.pow() method is used to calculate the power required by the compound-interest formula.
- The amount variable stores the final amount after compound interest.
- The compound interest is calculated by subtracting the principal from the final amount.
- System.out.println() displays the calculated values.
The important statement is:
double amount = principal * Math.pow((1 + rate / 100), time);
Here, Math.pow(x, y) calculates x raised to the power y.
Java Program to Calculate Compound Interest Using Method and Scanner Class
Problem Statement: Write a Java program to calculate compound interest dynamically by accepting the principal amount, annual rate of interest, and time period from the user via the Scanner class, utilizing a separate user-defined method for the calculation.
Java Program
// Import the Scanner class to accept user input
import java.util.Scanner;
public class CompoundInterestInteractive {
// User-defined method to calculate the total amount using the compound interest formula
public static double calculateAmount(double principal, double rate, double time) {
// Formula: A = P * (1 + R / 100)^t
return principal * Math.pow(1 + (rate / 100), time);
}
public static void main(String[] args) {
// Create a Scanner object to read input from the console
Scanner scanner = new Scanner(System.in);
// Prompt and accept the principal amount from the user
System.out.print("Enter the principal amount (₹): ");
double principal = scanner.nextDouble();
// Prompt and accept the annual rate of interest from the user
System.out.print("Enter the annual rate of interest (%): ");
double rate = scanner.nextDouble();
// Prompt and accept the time period in years from the user
System.out.print("Enter the time period (in years): ");
double time = scanner.nextDouble();
// Call the user-defined method to calculate the total amount
double amount = calculateAmount(principal, rate, time);
// Calculate the compound interest by subtracting the principal from the total amount
double compoundInterest = amount - principal;
// Display the final results formatted to two decimal places
System.out.println("\n--- Results ---");
System.out.printf("Principal Amount: ₹%.2f\n", principal);
System.out.printf("Total Amount: ₹%.2f\n", amount);
System.out.printf("Compound Interest: ₹%.2f\n", compoundInterest);
// Close the scanner to free up system resources
scanner.close();
}
}Expected Output:
Enter the principal amount (₹): 10000 Enter the annual rate of interest (%): 5 Enter the time period (in years): 2 --- Results --- Principal Amount: ₹10000.00 Total Amount: ₹11025.00 Compound Interest: ₹1025.00
Here is the step-by-step explanation of the program:
- Importing the Scanner Class: The program begins by importing java.util.Scanner, which is required to read input provided by the user from the console.
- User-Defined Method (calculateAmount): A user-defined static method is created with three parameters (principal, rate, and time) and computes the total amount using the compound interest formula alongside Math.pow().
- Main Method Execution: The main method acts as the entry point where the program execution begins.
- Initializing Scanner: A Scanner object is instantiated to read user inputs from the standard input stream.
- Accepting User Inputs: The program prompts the user to input the principal amount, annual interest rate, and time period, storing each value inside a double variable using nextDouble().
- Calling the Custom Method: The program invokes the calculateAmount method by passing the user’s inputs as arguments to calculate the final amount.
- Calculating Interest: The compound interest is derived by subtracting the initial principal amount from the calculated total amount.
- Displaying Results: System.out.printf() prints the results clearly, formatting the numbers to two decimal places for professional presentation.
- Closing the Scanner: Finally, scanner.close() is called to close the scanner resource and prevent memory leaks.





