#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    9
    Rep Power
    0

    C Program question...


    I have to write a program in C that will take the user input:

    Mortgage loan term,
    Loan amount,
    APR,
    Monthly payment,

    and print the payment number, interest paid per payment, principal paid per payment, and remaining balance, then at the end show the total interest paid and the remaining balance if any.

    I have all of it working great except I cannot get the balance to carry over and the interest for the next payment to calculate based on the new balance, thus the loop just shows each payment and final totals calculated as the same as the first.

    My trouble is figuring out how to word the syntax so it calculates the first payment's interest on the loan amount given and then replaces the original value with that new one for the subsequent calculations. I'm pretty sure I'm making this harder than I need to... Can you point me in the right direction to solve this?

    This is what I have so far:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
            int term;
            printf("How many years is your loan?\n");
            scanf("%i", &term);
    
            double amount;
            printf("What is the total loan amount?\n");
            scanf("%lf", &amount);
    
            double apr;
            printf("What is the annual interest rate?\n");
            scanf("%lf", &apr);
    
            double pmt;
            printf("What is the monthly payment?\n");
            scanf("%lf", &pmt);
    
            int n;
            (n = 0);
    
            double curr_mo_int;
            curr_mo_int = (apr / 100) / 12 * amount;
    
            double rem;
            rem = pmt - curr_mo_int;
    
            double sum;
            sum = 0, sum += curr_mo_int;
    
            double tot_bal_curr;
            tot_bal_curr = amount - rem;
    
    for (n=0; n < (term * 12); n++)
    {
    printf("Month (%i), Interest = %8.2f, Principal = %8.2f, Balance %10.2lf\n", n++, curr_mo_int, rem, tot_bal_curr);
    }
    for (n=0; n == (term * 12); n++);
    {
            printf("Total Interest Paid = %10.2lf, Remaining Balance = %8.2f\n", sum, tot_bal_curr);
    }
            return 0;
    }
    Thanks!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,181
    Rep Power
    2222
    Type the following:

    [code] [/code]

    Those are called "code tags". Their primary purpose is to preserve the indenting of your code listing and hence its formatting. Otherwise, HTML would strip out all the leading whitespace and render it unreadable. Code tags also create a window with scroll bars for the code.

    So then, in your new message type the code tags. Then in your editor, highlight your formatted code listing, copy it to the clipboard (eg, with Ctrl-C), and then paste it between the code tags in your message.

    It would be helpful if your editor is set to insert spaces instead of tab characters, because even if you've set your editor's tabs to something less than the default 8 columns, if you copy over tab characters then they will revert to the default 8 and your code will tend to march off the right side of the screen. This is not necessary like code tags are, but rather makes it nicer and easier to read.

    Thank you very much for asking for directions.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,904
    Rep Power
    481
    I would, and do think you ought to change the values of some of the variables within the loop.

    Code:
    while(condition){
      value = f(value, other, parameters);
      show(value);
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I would, and do think you ought to change the values of some of the variables within the loop.

    Code:
    while(condition){
      value = f(value, other, parameters);
      show(value);
    }
    Okay, I got the interest and principal to work, now I'm stuck on how to get the total of all the interest paid, so that all the interest amounts calculated get totaled at the end. I'm pretty sure it should be sum = but I'm not sure how to word the addition of all of the interest amounts...
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by Agalisgv
    Okay, I got the interest and principal to work, now I'm stuck on how to get the total of all the interest paid, so that all the interest amounts calculated get totaled at the end. I'm pretty sure it should be sum = but I'm not sure how to word the addition of all of the interest amounts...
    I think I finally got it! ^_^

IMN logo majestic logo threadwatch logo seochat tools logo