Jump to content

Practicing some C need help

SpaceAgentX

Hello, so I decided to start doing some C programming again to help refresh my memory. I decided to work on an amortization table for a loan. I found an example online of writing a program for calculating the interest rate for someone who buys a TV for 1000$ at an 18% interest rate with no down payment and a monthly payment of like 50$. I don't want to hard code the numbers.The program starts by asking the user for four numbers: The selling price, down payment, annual interest rate and the payment size.The program then displays a table like this that I have wrote up (with all payments displayed):


Pay #  Old Balance   Payment  Interest  To Balance  New Balance


-----      ----------------  ------------   ----------    --------------  ------------------


   1       1000.00          50.00         15.00          35.00             965.00


   2        965.00           50.00         14.48          35.52             929.48




  23         95.69           50.00         1.44            48.56             47.13


  24         47.13           47.84         0.71            47.13             0.00


-----------------------------------------------------------------------------------------


Total of payments =      1197.84


Total of interest =       197.84


Total to balance  =      1000.00


I have an old program I wrote up a few years back that's actually very similar but it's slightly different, asking the user to enter in the term of the loan and now I don't need them to do that. Though I can't recall that program working correctly. Can I use my old program that I have but change some things around, like the loop and variables? I'm not sure about the algorithm neither. I'll post the program I made the past in a pastebin link. I'm just really confused with how I can calculate the interest rate when I don't have them enter in how many months they have to pay. The program needs to tell them how many months it will take based on how much money they pay per month. Any help would be great.


http://pastebin.com/kam7sQE5


Link to comment
Share on other sites

Link to post
Share on other sites

I suggest writing out what you need to do, and what you want your program to do. Start from scratch and post an up date of what you have. Jumping into code after a long time of not coding can make things pretty tough. I Looked at your old code and the output was messy  :D present your self with clean output so you can keep track of things better.

[ Cruel Angel ]:     Exterior  -   BENQ XL2420T   |   SteelSeries MLG Sensei   |   Corsair K70 RED   |   Corsair 900D  |                                                                                                    CPU:    -   4.7Ghz @ 1.425v             |

                             Interior    -   i7 4770k   |    Maximus VI Formula    |   Corsair Vengeance Pro 16GB    |   ASUS GTX 980 Strix SLIx2  |  840 Pro 512Gb    |    WD Black 2TB  |           RAM:   -   2400Mhz OC @ 1.650v    |

                             Cooling   -   XSPC 120mm x7 Total Radiator Space   |   XSPC RayStorm    |    PrimoChill Tubing/Res  |                                                                                             GPU:   -   1000Mhz @ 1.158            |

Link to comment
Share on other sites

Link to post
Share on other sites

Okay I'll be honest I'm just lost. I know that for the calculation I need to do     

 

 

                          0.18     x       

          1000 * (1 + ------) ^       

                            12                 

 

that will give me 1015. Meaning i need to subtract 15 from 50$ giving me 35$ that goes towards the paying back the 1000$ and the 15$ goes towards the interest. I'm just struggling translating that into code and getting it to generate the table >< 

 

would this be the proper calculation?     PaymentAmount = (1+i/12)*n*(i/12*a)/(1+i/12)*n-1; where i = interest rate n = number of months and a = current balance 

Link to comment
Share on other sites

Link to post
Share on other sites

Okay so far I just have some variables. 

 
 
#include <stdio.h>
 
int main()
{
setbuf(stdout, NULL);
 
int SellingPrice,
DownPayment,
AmountOfLoan,
InterestRate,
MonthlyPayment;
 
 
printf ("Please enter in amount of loan\n");
scanf ("%i" , & AmountOfLoan);
printf ("Please enter down payment\n");
scanf ("%i" , & DownPayment);
printf ("Please enter the interest rate in decimal form\n");
scanf("%i" , & InterestRate);
printf ("Please enter in your monthly payment\n");
scanf ("%i" , & MonthlyPayment);
 
 
 
I'm honestly just lost. I'm struggling with figuring out how to translate the formula into code in the form of a loop to get it to generate that table 
Link to comment
Share on other sites

Link to post
Share on other sites

just know that i may say economically stupid things. beware

 

you have to pay monthly amounts until there is nothing else to pay

that translates into a loop, the exit condition will be a formula which has to mean "there is still something to pay"

 

every iteration of the cycle will apply the formula you described earlier to compute the AmountOfLoan value

knowing that, you will be able to update all the other variables

at the end of each iteration, the loop will show the results of the calculations as a row of the table

 

if some pseudo-code is needed, ask for it

Link to comment
Share on other sites

Link to post
Share on other sites

Yea I could use some of hat code lol anything would be of help

Link to comment
Share on other sites

Link to post
Share on other sites

sure, captain

read sellingPrice, monthlyPayment, interestRate // inputbalance = sellingPrice // i have to pay the whole thingwhile ( balance > 0)                                                                                             // loop as long as there is something to pay               begin loop                              interest = balance * interestRate / 12                              toBalance = monthlyPayment - interest                              balance = balance - toBalance                                              // pay! and travel towards the end of the loop               end loop

this pseudocode does no output, and it has kind of a bug in the last row, but still i hope it helped making it clear for you

working on this you should be able to solve your problem

Link to comment
Share on other sites

Link to post
Share on other sites

Hey guys this is what I have so far. When I run it, it doesn't end and freezes up my ide and I also don't have the table format yet b/c i'm really unsure of how to finish it. Can you guys help? 

 

 

#include <stdio.h>
#include <stdlib.h>
 
int main(void) {
 
setbuf(stdout,NULL);
int count = 0;
double sellingPrice, downPayment, loan, interestRate, monthlyPay, newBalance, interest, toBalance;
 
printf("Please enter the Selling Price \n");
scanf("%lf", &sellingPrice);
printf("Please enter your Down Payment \n");
    scanf("%lf", &downPayment);
    printf("Please enter the Interest Rate(as a decimal) \n");
    scanf("%lf", &interestRate);
    printf("Please enter your Monthly Payment \n");
    scanf("%lf", &monthlyPay);
    loan = sellingPrice - downPayment;
    newBalance = loan;
 
while(newBalance > 0)
{
double oldBalance = loan;
interest = (interestRate/12) * oldBalance;
toBalance = monthlyPay - interest;
oldBalance = newBalance;
count ++;
 
printf("%i", count);
printf("%lf", oldBalance);
printf("\t%lf", monthlyPay);
printf("\t%lf", interest);
printf("\t%lf", toBalance);
printf("\t%lf", newBalance);
 
}
 
 
return 0;
}
Link to comment
Share on other sites

Link to post
Share on other sites

the first error i can see is that at every iteration of the loop, the oldBalance is set to the loan, so it never ends

edit: and newBalance never changes, that's what causes the loop to never end, pardon

Link to comment
Share on other sites

Link to post
Share on other sites

okay so how can I fix that? lol sry I'm really just rusty at this

Link to comment
Share on other sites

Link to post
Share on other sites

int count = 0;double sellingPrice, downPayment, loan, interestRate, monthlyPay, newBalance, interest, toBalance, oldBalance, totalInterest; // everything declared hereprintf("Please enter the Selling Price \n");scanf("%lf", &sellingPrice);/*no? isn't this the same as monthlyPay?printf("Please enter your Down Payment \n");scanf("%lf", &downPayment);*/printf("Please enter the Interest Rate(as a decimal) \n");scanf("%lf", &interestRate);printf("Please enter your Monthly Payment \n");scanf("%lf", &monthlyPay);// note: oldBalance is declaired outside of the loop. otherwise, the variable will be destroyed at every iteration and lose its valuenewBalance = loan;totalInterest = 0.0;while(newBalance > 0.0){	// calculate interest and the part to balance	interest = ( interestRate / 12 ) * newBalance;	toBalance = monthlyPay - interest;	totalInterest = totalInterest + interest;	// update the balance	oldBalance = newBalance;	newBalance = oldBalance - toBalance;	count ++;	printf("%i", count);	printf("%f", oldBalance);	printf("\t%lf", monthlyPay);	printf("\t%lf", interest);	printf("\t%lf", toBalance);	printf("\t%lf", newBalance);}

consider that i can't test the code, i don't have a compiler, so don't be surprised if there are some errors

 

 

wait if I change loan to newbalance it should end right?

no, because as i explained in this example the variable oldBalance in your code does not keep its value: it's destroyed every time the loop ends and restarts

Link to comment
Share on other sites

Link to post
Share on other sites

Okay what you gave me does indeed worked but I got a negative at the end and i want it to be a 0 I'm also confused on how to get it formatted into a neatly labeled table oh and what about rounding all my dollar amounts to the nearest cent? 

Link to comment
Share on other sites

Link to post
Share on other sites

referring to your example, at the last iteration the balance is 47.13, but your (my?) code doesn't really give a shit, and tries to pay 50 bucks anyway, therefore you get a negative balance at the end

in the loop, you should check if the balance is lower than the monthly pay. in that case, you should only pay what there is left to pay

 

regarding decimal places, http://lmgtfy.com/?q=printf+double+decimal+places

first result FTW

Link to comment
Share on other sites

Link to post
Share on other sites

okay I got the decimals. So I should use an if statement? If totalBalnce < monthlyPayment then totalBalnce = monthlyPayment? 

Link to comment
Share on other sites

Link to post
Share on other sites

mmm kinda

 

if you change totalbalance to the monthly payment, you will end up paying 50 bucks instead of the $47.13 that you should pay

you have to do the opposite, but be careful to what happens with the interest and the actual payment

Link to comment
Share on other sites

Link to post
Share on other sites

is if (totalBalance < monthlyPayment, monthlyPayment = totalBalance + interest) the correct syntax?

Link to comment
Share on other sites

Link to post
Share on other sites

nope

 

and, if i wanted to be a boring annoying dude, i'd warn you that the amount of

totalBalance + interest

may exceed the monthlypayment, so you could end up paying more than 50 bucks in a month

Link to comment
Share on other sites

Link to post
Share on other sites

ok so I took out the + interest part. I guess I just don't know the proper syntax for this. 

Link to comment
Share on other sites

Link to post
Share on other sites

 

 

and, if i wanted to be a boring annoying dude

Since I do want to be a boring annoying dude, I'll warn you that you shouldn't be using floats/doubles for money calculations.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

is if(monthlyPayment > totalBalance) monthlyPayment = totalBalance;

the correct syntax? 
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×