Java Program to Calculate Compound Interest

Formula to calculate compound interest

Java programs to calculate compound interest have been shown here. Both of the iterative and recursive approaches have been covered. Computing compound interest using formula has also been shown here.

The following section covers the Python programs to calculate compound interest.






1. Java Program & output to calculate compound interest using formula

Code has been copied
/***************************************************************
            		alphabetacoder.com
	Java program to calculate compound interest using formula
****************************************************************/
import java.util.Scanner;
import java.lang.Math;

public class Main {	
    public static void main(String []args){
	// declare instance of Scanner class
	Scanner sc = new Scanner(System.in);
	// declare variables
	double ci, amount, p, r;
	int n, t;
	
	// take input from user 
	System.out.print("Enter the principal balance = ");
	p = sc.nextDouble();
	System.out.print("Enter the annual interest rate = ");
	r = sc.nextDouble();
	System.out.print("Enter compound frequency / year = ");
	n = sc.nextInt();
	System.out.print("Enter the year  = ");
	t = sc.nextInt();
	
	// calculate compounded value
	// using formula
	amount = p * Math.pow((1 + r/(100 * n)), n*t);
	
	//find the compound interest
	ci = amount - p;
	
	// display output out 2 decimal places
	System.out.println("Compound interest = "+Math.round(ci * 100.0) / 100.0);
    }
}

Output


Enter the principal balance = 11000

Enter the annual interest rate = 6

Enter compound frequency / year = 2

Enter the year = 10

Compound interest = 8867.22




2. Java Program & output to calculate compound interest using loop (Iteration)

Code has been copied
/***************************************************************
            		alphabetacoder.com
Java program to calculate compound interest using loop (Iteration)
****************************************************************/
import java.util.Scanner;
import java.lang.Math;

public class Main {	
    public static void main(String []args){
	// declare instance of Scanner class
	Scanner sc = new Scanner(System.in);
	// declare variables
	double ci, amount, p, r;
	int n, t, i;
	
	// take input from user 
	System.out.print("Enter the principal balance = ");
	p = sc.nextDouble();
	System.out.print("Enter the annual interest rate = ");
	r = sc.nextDouble();
	System.out.print("Enter compound frequency / year = ");
	n = sc.nextInt();
	System.out.print("Enter the year  = ");
	t = sc.nextInt();
	
	// initialize
        amount = p;
    
        // calculate compounded value using loop
        for(i = 0; i < n * t; i++)
    	    amount = amount + amount * (r / (n * 100));
	
	//find the compound interest
	ci = amount - p;
	
	// display output out 2 decimal places
	System.out.println("Compound interest = "+Math.round(ci * 100.0) / 100.0);
    }
}

Output


Enter the principal balance = 11000

Enter the annual interest rate = 6

Enter compound frequency / year = 2

Enter the year = 10

Compound interest = 8867.22




3. Java Program & output to calculate compound interest using recursion

Code has been copied
/***************************************************************
            		alphabetacoder.com
Java program to calculate compound interest using recursion
****************************************************************/
import java.util.Scanner;
import java.lang.Math;

public class Main {	
    // recursive function to compute compound interest
    static double compound_interest(double p0, double p, double r, int n, int t, int itr){
	// if number of total no of compound reached
	if(itr == n * t)
	    return (p - p0);
	// calculate interest
	double interest = p * (r / (n * 100));
	
	// call function
	return compound_interest(p0, p + interest, r, n, t, itr + 1);
    }

    public static void main(String []args){
	// declare instance of Scanner class
	Scanner sc = new Scanner(System.in);
	// declare variables
	double ci, p, r;
	int n, t;
	
	// take input from user 
	System.out.print("Enter the principal balance = ");
	p = sc.nextDouble();
	System.out.print("Enter the annual interest rate = ");
	r = sc.nextDouble();
	System.out.print("Enter compound frequency / year = ");
	n = sc.nextInt();
	System.out.print("Enter the year  = ");
	t = sc.nextInt();
	
	// call function to compute compound interest
        ci = compound_interest(p, p, r, n, t, 0);
	
	// display output out 2 decimal places
	System.out.println("Compound interest = "+Math.round(ci * 100.0) / 100.0);
    }
}

Output


Enter the principal balance = 90000

Enter the interest rate = 7

Enter compound frequency / year = 4

Enter the year = 20

Compound interest = 270575.27