Thread: while loop help

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    9

    while loop help

    I have been to use a simple loop to count the number of months it takes to pay off a loan, and it should look like this:

    Enter a principal amount:
    10000
    Enter an annual interest rate:
    12
    Your first month of interest will be $100.00
    Enter the amount of your monthly payment:
    200
    You borrowed: $10000.00
    Your payment schedule consists of 69 payments of $200.00
    and a final principal payment of $131.06.
    The total amount of interest you pay will be $3931.06.

    i have figured out how to do everything except the last three lines. i know how to make math formulas up to calculate it but I am supposed to use a "simple loop" to do it instead. i dont know how to make the loop to figure out the total payments, final principal payments, and total interest.

    Here is what i have:
    Code:
    #include <stdio.h>
    int main(){
    
    double principal, interest_rate, payment, payments, total_interest;
    
    printf("Enter a principal amount:\n");
    scanf("%lf", &principal);
    
    printf("Enter an annual interest rate:\n");
    scanf("%lf", &interest_rate);
    
    printf("Your first month of interest will be: %0.2lf\n", (12 / interest_rate * .01) * principal);
    
    printf("Enter the amount of your monthly payment:\n");
    scanf("%0.2lf", &payment);
    
    printf("You borrowed: %0.2lf\n" principal);
    
    while(
    {
    
    }
    
    }
    i have been plugging things into the while loop, and have tried numerous for loop combinations but nothing works for me.

  2. #2
    Dead-Eternity dead-eternity's Avatar
    Join Date
    Mar 2005
    Location
    Quebec, Canada
    Posts
    4
    I have an idea but i dont know if i understand the problem / question

    Code:
     
    int k = 0;
    double total;
    
    total = principal;
    
    While (total > payment)
     {
    //calculating total interest
    total_interests = total_interests + (total x 12 / interests x 0.01); 
    //calculating remaining payments
    total = (total x (1 + (12 / interests x 0.01)))  - payment;
    //increasing the value of done payments
    ++k;
    }
    printf("Your payment schedule consists of %d payments of %lf \n", k, payment);
    printf("and a final principal payment of %c%lf.\n", '$', total);
    printf("The total amount of interest you pay will be %c%lf.\n", '$', total_interests);

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    thanks for the nice attempt but when i entered your information in to my code, it came up with about 20+ errors.

    is it possible to do this with a for loop instead of a while loop?

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Code:
    (12 / interest_rate * .01) * principal)
    why dont you simplify that and make it a function, it will make your code more readable and more understandable as to what is going on in the code.
    When no one helps you out. Call google();

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    Dead eternity's code looks good, it just isn't part of your program yet. You need to be sure all the variables are named the way you want them and have all been declared. Yes, of course, it's possible to do it as a do-while loop or as a for loop, but why do you care in this case? I think we should leave that as an exercise for you after you get this version working.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    ok i have this much for the loop part and i feel it is correct

    Code:
      total_payment = 0;
    
      while( principal > total_payment )
      {
      current_interest = interest_of(principal - total_payment);
      principal = principal + current_interest - payment;
    
      total_payment += payment;
    
      if( principal < 0 )
        final_payment = payment - principal;
    
      months++;
      }
    just for where it says "interest_of". i can't figure out wht to put in to get it to figure out the changing interest rate. does this look right, and what may i be missing? Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM