AwkwardMonkey - 1 year ago 45

Java Question

Basically in this website you can get credits and buy stuff with them. If you keep the credits in your bank, you get 0.12% added daily:

I wanted to code a quick program to calculate this but I need help, here's my code:

`import java.util.Scanner;`

public class project {

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.println("-------------------------");

System.out.println("| NextGenUpdate Credits |");

System.out.println("-------------------------");

System.out.println("Credits: ");

double credits = input.nextDouble();

System.out.println("Days: ");

int days = input.nextInt();

int total = //DON'T KNOW WHAT TO PUT HERE

System.out.println("You will have have " + total + " credits in " + days + " days.");

}

}

Answer Source

I guess that you need to calculate the total credits after `days`

days. For this, I would suggest `total`

to be of `double`

type, since it will most probably have floating point values.

You can do:

```
double total = credits * Math.pow((1+ 0.12/100d), days);
```

The formula for compounding is :

A = P ( 1 + R/100)^n

Thus, your R is 0.12, P is `credits`

, and n is `days`

.

But since you want to use `int`

, hence the value after decimal will be lost.

eg: 12.98 will give 12.

So, Instead, you can round off the value. You may use:

```
int total = (int)Math.rint(credits * Math.pow((1+ 0.12/100d), days));
```