Reputation: 9
so, I've tried multiple different things, but I am still getting an answer of infinity when I run this for the last output, what do you think tht the problem is? The first output works fine, but the second and third do not. he exponent is supposed to be ^12*5
. Am i not doing the math.pow
right?
import java.util.Scanner;
public class CompoundInterest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double principal = 0;
double rate = 0;
double time = 0;
double one = 0;
double three = 0;
double five = 0;
double a, b;
System.out.print("Enter the amount of the loan: ");
a = input.nextDouble();
System.out.print("Enter the Rate of interest : ");
b = input.nextDouble();
one = a * Math.pow((1 + b/12),Math.pow(12,1));
three = a * Math.pow((1 + b/12),Math.pow(12,1));
five = a * Math.pow((1 + b/12),Math.pow(12,5));
System.out.println("");
System.out.println("The Compound Interest after 1 year is : "
+ one);
System.out.println("");
System.out.println("The Compound Interest after 3 years is : "
+ three);
System.out.println("");
System.out.println("The Compound Interest after 5 years is : "
+ five);
}
}
Upvotes: 0
Views: 869
Reputation: 11961
You are actually taking two powers, for example in the second line (the one for five years interest):
five = a * Math.pow((1 + b/12),Math.pow(12,5));
which comes down to: a * (1 + b/12)^(12^5)
. This is a number close to infinity on normal 32 or 64 bit computers.
Try using a * Math.pow((1 + b/12), 12 * years);
where years
is the number of interest years.
Upvotes: 4
Reputation: 21934
You are raising something to the power of 12^5
, with is absurdly huge and should result in infinity.
Try
five = a * Math.pow((1 + b/12), 12*5);
instead.
Upvotes: 1