Calculation Question

This is a discussion on Calculation Question within the C Programming forums, part of the General Programming Boards category; So, while I think I am getting better (I THINK), one thing that will always stump me in programming are ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    62

    Calculation Question

    So, while I think I am getting better (I THINK), one thing that will always stump me in programming are equations. More specifically, longer mathematical equations. Right now, I am making a "Loan Calculator". Basically, it just uses a switch in another function, returns the values to the main function and it prints it out.

    Unfortunately, given the formula I need to use, I can't figure out how to incorporate it in into a loop, as well I can't really understand the equation, even though it explains it.. (I'm retarded when something involves complicated looking math.)

    Here's my code thus far. I need to make a loop do two things::

    --Loop to output for however many months entered and the information for each month (Posted guess at end of post) This isn't my major issue however.

    --Figure out how exactly the equation works and use it in the loop (My major issue).
    ----EQUATION:: payment =.......... ........principal x rate....
    .................... .... .......... 1 - ( (1+rate) to the power (-m) ) )

    --I'v also got no idea what a "Principal" refers to.

    Rate = annual interest rate
    M = number of months

    (I know it's not done but. I can't really do much unless I figure this calculation stuff out
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int menu(void);
    int swit(void);
    
    int main(void)
    {
        
        int choice;
        
        printf("1. Enter Principal\n2. Enter Annual Interest Rate\n3. Enter Duration of the loan\n4. Calculate Loan Payment\n5. Display Loan Repayment Table\n\n");
        
        choice = menu();
        
        
        printf("Month::Old Balance::Payment::Interest::Principal::New Balance");
        
        
        getchar();
        getchar();
        return 0;
    }
    
    
    
    
    int menu(void)
    {
        int choice;
        int princ;
        int ir;
        int dur;
        int calc;
        
        printf("Make your choice from the above menu:: ");
        
        while ((choice = getchar()) != 0)
        {
    
    
        switch (choice)
        {
               
               case '1' :
                    
                    princ = getchar();
                    break;
               case '2' :
                   
                    ir = getchar();
                    break;
               case '3' :
                    
                    dur = getchar();
                    break;
               case '4' :
                    
                    break;
               case '5' :
                    
                    break;
               default :
                       printf("You entered an invalid choice.\nPlease pick 1-5 or use 0 to exit the program\n");
                       break;
        }
    }
        return choice;
        
    }
    So far my guess is going to be something like this for the loop

    for (i = 1; i < months; i++). However. I am somewhat confused on how to do the calculations themselves (This is the most major thing I need help with. How exactly, after each calculation, I am supposed to re-calculate for the loop.

    Any help would be greatly appreciated. I am absolutely useless when it come's to math.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Code:
    for(i = 0;i < months;++i)
      principal = principal * (1 + (rate / 12));
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by itsme86 View Post
    Code:
    for(i = 0;i < months;++i)
      principal = principal * (1 + (rate / 12));
    If they input 36 for however many months they put in for time to pay, than I assume that means this can easily be altered by dividing rate / 36, rather than 12?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    You don't need to change it. The division by 12 is just to change the annual interest rate to a monthly calculation. If the annual interest rate is, let's say, 6%, the monthly interest you pay should only be 6% / 12, or 0.5%, per month.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by itsme86 View Post
    You don't need to change it. The division by 12 is just to change the annual interest rate to a monthly calculation. If the annual interest rate is, let's say, 6%, the monthly interest you pay should only be 6% / 12, or 0.5%, per month.
    Alright thanks alot. Helped me out big time. And looked like what I thought the loop would be was only slightly off. Thanks again ^^

    So i'v not got a new question about the calculation.

    Using this:

    Code:
        for(int i = 0;i < months; ++i)
        {
            printf(" new balance: %d ", newbalance);
            principal = principal * (1 + (rate / 12));
            buffer=oldbalance-principal;
            newbalance=buffer;
        }
    Given values for each variable, the program does as it should for the most part, however. I am missing one small thing. The "rate" variable doesn't seem to change anything. even given the variable a value, won't change the value. It will just consistently subtract 500 each time until the loop finishes, depending on the months entered. Anything i'm missing here?
    Last edited by NinjaFish; 04-07-2011 at 06:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 03:23 PM
  3. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  4. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21