# Basic Java programming exercises

 Java Tutorial Java Exercises Data Structures

## Java program to calculate Compound Interest

Write a Java program to input principle (amount), time and rate (P, T, R) and find Compound Interest. How to calculate compound interest in Java programming. Logic to calculate compound interest in Java program.

##### Required knowledge

Variables and expressions,Arithmetic operators,Basic input/output,Data types

##### Compound Interest formula

Formula to calculate compound interest annually is given by. Where,

P is principle amount

R is the rate and

T is the time span

##### Required knowledge for logic to calculate compound interest

Step by step descriptive logic to find compound interest.

1. Input principle amount. Store it in some variable say principle.

2. Input time in some variable say time.

3. Input rate in some variable say rate.

4. Calculate compound interest using formula, CI = principle * pow((1 + rate / 100), time).

5. Finally, print the resultant value of CI.

##### Program to calculate compound interest
```/**
* Java program to calculate Compound Interest
*/
import static java.lang.Math.pow;
import java.util.Scanner;
class Test
{
public static void main(String args[])
{

double amount=0,principle,rate,time,ci,t=1;
Scanner op=new Scanner(System.in);
/* Input principle, time and rate */
System.out.println("enter principle ");

principle=op.nextDouble();

System.out.println("enter rate");

rate=op.nextDouble();

System.out.println("enter time");

time=op.nextDouble();
/* Calculate compound interest */
rate=(1+rate/100);

for(int i=0;i<time;i++)
t*=rate;

amount=principle*t;

System.out.println("amount="+amount);

ci=amount-principle;

System.out.println("compound intrest="+ci);

}
}
```

##### Output:

```enter principle
9800
enter rate
2
enter time
8
amount=11482.261933822205
compound intrest=1682.2619338222048

```

```/**
* Java program to calculate Compound Interest
*/
import static java.lang.Math.pow;
import java.util.Scanner;
class Test
{
public static void main(String args[])
{

Double principle, rate, time, CI;
Scanner op=new Scanner(System.in);
/* Input principle, time and rate */
System.out.print("Enter principle (amount): ");
principle=op.nextDouble();

System.out.print("Enter time: ");
time=op.nextDouble();

System.out.print("Enter rate: ");
rate=op.nextDouble();

/* Calculate compound interest */
CI = principle* (pow((1 + rate / 100), time));

/* Print the resultant CI */
System.out.println("Compound Interest = "+CI);

}
}
```

##### Output:

```Enter principle (amount): 10500
Enter time: 2
Enter rate: 2
Compound Interest = 10924.2
```