Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 20th, 2005, 6:05 PM   #1
bliznags
Newbie
 
Join Date: Mar 2005
Location: Bethany Beach, DE
Posts: 10
Rep Power: 0 bliznags is on a distinguished road
my loop wont work right.... please help

my program needs to print out the following results:
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

it is all fine in my code up until it calculates to print this line:
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

instead i am getting:
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

here is my 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;

}

the math in my loop has to be what is doing it, but i dont see why....
can anyone clear my problem up for me please?

thank you
bliznags is offline   Reply With Quote
Old Mar 20th, 2005, 6:33 PM   #2
gardon
Programmer
 
Join Date: Dec 2004
Posts: 87
Rep Power: 4 gardon is on a distinguished road
I don't have time to look at that right now. I'm about to eat dinner, but as soon as I get back I'll try to take a look at it.
gardon is offline   Reply With Quote
Old Mar 20th, 2005, 8:02 PM   #3
Mad_guy
Hobbyist Programmer
 
Mad_guy's Avatar
 
Join Date: Oct 2004
Location: Sandstorm, Techno Club
Posts: 239
Rep Power: 4 Mad_guy is on a distinguished road
Send a message via AIM to Mad_guy Send a message via MSN to Mad_guy
Whoops! Wrong topic. :p

Just a second, I'll look into it...

Last edited by Mad_guy; Mar 20th, 2005 at 8:06 PM.
Mad_guy is offline   Reply With Quote
Old Mar 20th, 2005, 8:36 PM   #4
bliznags
Newbie
 
Join Date: Mar 2005
Location: Bethany Beach, DE
Posts: 10
Rep Power: 0 bliznags is on a distinguished road
thanks guys, can't wait to see what you say
bliznags is offline   Reply With Quote
Old Mar 21st, 2005, 12:06 AM   #5
Ramlag
Newbie
 
Join Date: Mar 2005
Posts: 18
Rep Power: 0 Ramlag is on a distinguished road
printf("Your first month of interest will be: %.2lf\n", annual_interest_rate / 12 * (initial_principal / 100));
This caught my eye.

I dont know if this is helpfull or not cause im a nub at this, and hell im probably wrong anyway.

But if theres a string of numbers say like ... 10 * 10 + 10 / 2 youd think the answer was like ... 55, but in C its not, the answer would be ... 60 (i think thats right). C calculates the multiplication and division first so youd need to space that sum like this ... ((10 * 10) + 10 (/ 2)) i think ... sorry if this isnt too clear but like i said im a nub at this.
Ramlag is offline   Reply With Quote
Old Mar 21st, 2005, 3:11 AM   #6
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
bodmas

brackets, other, multiplication, addition subtraction :/ order of arithmatic.

and ummm 7% of 2000 is not 11.67 its 140

you sure it is 11.67, is thisinterest on the 2000 or just come random number?

and is this ment ot be compound interest? i would guess it is... for that you need to add each months interest amount onto the total then re calculate the interest on that amount.

I had a look at the problem adn you need to look at your maths i need to see what the actual question is before i can help you really :/
Berto is offline   Reply With Quote
Old Mar 21st, 2005, 7:14 AM   #7
bliznags
Newbie
 
Join Date: Mar 2005
Location: Bethany Beach, DE
Posts: 10
Rep Power: 0 bliznags is on a distinguished road
no, that is not the problem. i had to figure out the dollar amount of the first months interest off of the annual interest rate, therefore it is a small number. the output i should be getting for the problem is this, it is printed out on the sheet in front of me:

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

that is what need to print out when i run the program and enter those numbers, instead i am getting this at the end:

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.

so its obvious my problem is the math inside the loop, and i have no idea how to figure it out. i have worked on it forever, so if someone could guide me in the right direction id really appreciate it.

Last edited by bliznags; Mar 21st, 2005 at 7:19 AM.
bliznags is offline   Reply With Quote
Old Mar 21st, 2005, 8:16 AM   #8
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
well here is my code


#include <stdio.h>
int main()
{
  double                   initial_principal = 0, 
                           annual_interest_rate = 0, 
                           monthly_payment = 0, 
                           total = 0, 
                           interest = 0,
                           total_payments = 0, 
                           annuity = 0, 
                           total_interest = 0,
                           interest_rate = 0;
  
  int                      years_annuity = 0, 
                           months = 0, 
                           payments = 0;


  printf("Enter an initial deposit amount:\n\n");
  scanf("%lf", &initial_principal);

  printf("Enter an annual interest rate:\n");
  scanf("%lf", &annual_interest_rate);
 
//  interest_rate = annual_interest_rate / 12 * (initial_principal / 100);//new
  printf("Your first month of interest will be: %.2lf\n", (annual_interest_rate / 12 * (initial_principal / 100)));//new

  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); 




  total = initial_principal;
  while ( months < years_annuity * 12 )
   {
     if (months != 0) total = total + monthly_payment; //adds the monthly payment for all months??? did you need it like this?
     total_interest = total_interest + (total * ((annual_interest_rate /12)/100));//works out the interest on the current new total amount
     total = total + (total * ((annual_interest_rate /12)/100));
                                                              //adds the amount to the current total
//     printf("%.2lf\n", interest);
  
     total_payments = total_payments + monthly_payment;      //total payments how could you go so wrong with this
                                                             //just add payments to total amount.
     //printf("%.2lf\n", total_payments);
   
     ++months;
   }
  
  annuity = total_payments + total_interest + initial_principal;//final total = interes (total amount + payments)? which is wrong eep

  printf("You started with a principle of: $%.2lf\n",initial_principal);
  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", total_interest);
  printf("At the end of %d years your annuity will be worth $%.2lf.\n",years_annuity, annuity);

  return 0;

}

here are the changes most in the while loop, you need to work out your maths better.....

Also the reason you get -Nan is due to not initialising variables before you use them, it causes all kinds of headaches if you dont.

Last edited by Berto; Mar 21st, 2005 at 8:24 AM.
Berto is offline   Reply With Quote
Old Mar 21st, 2005, 11:19 AM   #9
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
  //initialize annuity
  annuity = initial_principal;

  while ( months < years_annuity * 12)
   {
     //calcualte this months interest based on annuity
     interest = (annual_interest_rate/12/100) * annuity;

     //keep running total of interest for displaying later
     total_interest += interest;

     //update annuity
     annuity = annuity + interest + monthly_payment;

     //keep running total of payments for displaying later
     total_payments += monthly_payment;

     ++months;
   }
spydoor is offline   Reply With Quote
Old Mar 21st, 2005, 1:29 PM   #10
bliznags
Newbie
 
Join Date: Mar 2005
Location: Bethany Beach, DE
Posts: 10
Rep Power: 0 bliznags is on a distinguished road
thanks so much for the help guys. i like how two of you posed two different approaches to the problem.
bliznags is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:37 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC