![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Location: Bethany Beach, DE
Posts: 10
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Dec 2004
Posts: 87
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
Whoops! Wrong topic. :p
Just a second, I'll look into it... Last edited by Mad_guy; Mar 20th, 2005 at 8:06 PM. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Mar 2005
Location: Bethany Beach, DE
Posts: 10
Rep Power: 0
![]() |
thanks guys, can't wait to see what you say
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2005
Posts: 18
Rep Power: 0
![]() |
printf("Your first month of interest will be: %.2lf\n", annual_interest_rate / 12 * (initial_principal / 100));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. |
|
|
|
|
|
#6 |
|
Programming Guru
![]() |
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 :/ |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Mar 2005
Location: Bethany Beach, DE
Posts: 10
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#8 |
|
Programming Guru
![]() |
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. |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
//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;
} |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Mar 2005
Location: Bethany Beach, DE
Posts: 10
Rep Power: 0
![]() |
thanks so much for the help guys. i like how two of you posed two different approaches to the problem.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|