Thread: please help me with this loop

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    9

    please help me with this loop

    ive been asked the following question.

    Start with an initial principal amount and a fixed payment amount. Every month, the payment amount is added to the principal, along with th interest that the principal has earned for the past month, so the principal grows in two ways. Show this output:

    enter an initial depost:
    2000
    Enter an annual interest rate
    7
    your first month of interest will be 11.67
    enter the number of years your annuity will be in the bank:
    30
    enter the amount of your monthly payment
    200
    you started with a principal of 2000.00
    your payment schedule consists of 360 payments of 200.00
    the total amount of your payments will be 72000.00
    the total amount of interest you will earn will be 186227
    at the end of 30 years your annuity will be 260227.19

    here is what im getting though:

    enter an initial depost:
    2000
    Enter an annual interest rate
    7
    your first month of interest will be 11.67
    enter the number of years your annuity will be in the bank:
    30
    enter the amount of your monthly payment
    200
    you started with a principal of 2000.00
    your payment schedule consists of 360 payments of 200.00
    The total amount of your payments will be $-200.00.
    The total amount of interest you earn will be $4200.00.
    At the end of 1085227007 years your annuity will be worth $-NaN.

    My code is as follows:
    Code:
    #include <stdio.h>
    int main()
    {
      double initial_principal, annual_interest_rate, monthly_payment, total, interest = 0,
        total_payments, annuity;
      
      int years_annuity, months = 0, payments = 0;
    
      total = initial_principal;
    
      printf("Enter an initial deposit amount:\n\n");
      scanf("%lf", &initial_principal);
    
      printf("Enter an annual interest rate:\n");
      scanf("%lf", &annual_interest_rate);
     
      printf("Your first month of interest will be: %.2lf\n", annual_interest_rate / 12 * (initial_principal / 100));
    
      printf("Enter the number of years your annuity will be in the bank:\n");
      scanf("%d", &years_annuity);
    
      printf("Enter the amount of your monthly payment:\n");
      scanf("%lf", &monthly_payment); 
    
      printf("You started with a principal of $%0.2lf\n", initial_principal);
    
      while ( months < years_annuity * 12 )
       {
         interest = interest + (annual_interest_rate / 12 * (initial_principal / 100));
         printf("%.2lf\n", interest);
      
         total_payments = (total * (1 + (12 / annual_interest_rate * .01))) - monthly_payment;
         printf("%.2lf\n", total_payments);
       
         ++months;
       }
      
      annuity = interest + total_payments + total;
    
      printf("Your payment schedule consists of %d payments of $%.2lf,\n", months, monthly_payment);
    
      printf("The total amount of your payments will be $%.2lf.\n", total_payments);
    
      printf("The total amount of interest you earn will be $%.2lf.\n", interest);
    
      printf("At the end of %d years your annuity will be worth $%.2lf.\n", annuity);
    
      return 0;
    
    }
    my problem lies inside the loop with my math calculations. I've been working on this forever but cant figure out the right equations to get it to print out the desire results. if anyone knows what could be causing this please enlighten me because i can't figure it out.

    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Code:
    printf("Your payment schedule consists of %d payments of $%.2lf,\n", months, monthly_payment);
    and
    Code:
    printf("At the end of %d years your annuity will be worth $%.2lf.\n", annuity);
    you see the difference between them? what does %d give you on the satemetn above? Also NAN means Not a Number so one of your calculations is wrong.
    When no one helps you out. Call google();

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    you get NaN becuase your last printf statment expects two variables and you only pass one.

    Now about your calculations

    to keep track of your total payments you simply need

    Code:
    total_payments = total_payments + monthly_payment;

    you need to keep track of your annuity as you go, and apply the interest_rate to your current annuity

    annuity should start as your initial_principal.

    your interest for each month is
    Code:
    interest = interest_rate * annuity
    then update your annuity
    Code:
    annuity = annuity + interest + monthly_payment
    you also need another variable for total_interest.. this isn't used in a calculation but just to display later.

    one more thing

    when you do this
    Code:
    annual_interest_rate / 12 * (initial_principal / 100))
    you should proably actually be doing
    Code:
    annual_interest_rate / 12 /100 * (initial_principal ))
    it works out the same, but makes more sense to convert your interest rate to a decimal
    Last edited by spydoor; 03-21-2005 at 09:03 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM