# Program to calculate Compound Interest in Java?

0

Program to calculate Compound Interest in Java?

This is a common program that will be asked by multiple interviewers from freshers and experienced both.

most of the students work on SI but they not comfortable with SI Calculation but not know Compound Interest Calculation easily.

We will use Math.pow() function to calculate CI.

Math.pow() has two parameters, the first parameters will be base and the second parameters will be an exponent.

Math.pow(base,exponent)

Formula will be  = principal * (Math.pow(1+(r/100),n*t)

Complete program to calculate compound interest?

import java.util.Scanner;

class CI

{

public static void main(String args[])

{

float p,r,t,n,a;

double total;

Scanner sc = new Scanner(System.in);

System.out.println("Enter p");

p=sc.nextFloat();

System.out.println("Enter r");

r=sc.nextFloat();

System.out.println("Enter t");

t=sc.nextFloat();

System.out.println("Enter number of time for interest");

n=sc.nextInt();

total = p*(Math.pow((1+r/100),(n*t)));

System.out.printf("actual amount is %.2f, Total amount %.2f, Total interest

is %.2f ",p,total,(total-p));

}

}

Tags