1

I am trying to write a payment calculator program, but am receiving "NaN" as my result. The program asks for input on the loan amount and length of loan (in months). The program should then calculate out the monthly payments for each APR (3% -10%). I'm not sure if I have done something wrong with my calculation.

   double L, payment;   
   double APR = 0;   
   int n;   
   Scanner input = new Scanner(System.in);    
   System.out.println("Loan calculator");   
   System.out.print("Enter the loan amount: ");   
   L = input.nextDouble();   
   System.out.print("Enter the number of payments: ");  
   n = input.nextInt();   
   double t = APR/1200.0;      
   for (APR = 3; APR <= 10; APR += .25){   
      payment = L * (t * (Math.pow((1.0 + t), n)) / (Math.pow((1.0 + t), n) - 1.0));
      System.out.println(APR + "\t" + payment);
Progman
  • 16,827
  • 6
  • 33
  • 48
ASR0857
  • 21
  • 2
  • 3
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – Progman Oct 25 '19 at 20:15
  • 5
    `t` is always 0. Now, find out why ... If `t` is always 0, then `(Math.pow((1.0 + t), n)` is always 1.0, then 1.0-1.0 is always 0.0 and dividing by 0 is possibly NaN – Thomas Weller Oct 25 '19 at 20:17

1 Answers1

0

Define or at least assign t within the loop. Otherwise it will only be computed once before the loop using APR, which is 0 at that time.

for (APR = 3; APR <= 10; APR += .25){   
   double t = APR/1200.0;      
   payment = L * (t * (Math.pow((1.0 + t), n)) / (Math.pow((1.0 + t), n) - 1.0));
   System.out.println(APR + "\t" + payment);
fedup
  • 1,209
  • 11
  • 26