Thread: Can't stop my program at zero (Loan Table)

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    5

    Can't stop my program at zero (Loan Table)

    Ok I had to make a loan table where you enter the loan amount, the monthly interest rate, and the amount of the monthly payment, it then prints out a table containing the month, payment, interest, and remaining

    I can't get the program to stop when remaining = 0

    Here is my code:
    Code:
    #include <stdio.h>
    
    int main()
    
    {
        double loan_amt;
        double interest_rate;
        double remaining;
        double interest;
        double payment;
        int month = 1;
       
        printf("Enter the loan amount\n");
        scanf("%lf", &loan_amt);
        printf("Enter the monthly interest rate of the loan\n");
        scanf("%lf", &interest_rate);
        printf("Enter the amount of the monthly payment\n");
        scanf("%lf", &payment);
       
        printf("+-------+---------+-----------+----------+\n");    
        printf("| Month | Payment | Interest  | Remaining | \n");
        printf("+-------+---------+-----------+----------+\n");
       
        interest_rate = (loan_amt*(interest_rate/100));
       
    while(loan_amt > 0)
        { 
          interest = (loan_amt*(interest_rate/100)); 
          loan_amt = loan_amt+interest; 
    
          printf("| %3d   | %5.2lf  | %4.2lf     | %.2lf   |\n",
          month, payment, interest, loan_amt);
          month++;
                if(payment > (loan_amt + interest)) 
          {
            interest = (loan_amt*(interest_rate/100));   
            payment = (loan_amt + interest);   
            loan_amt = 0; 
            printf("| %3d   | %5.2lf  | %4.2lf     | %.2lf   |\n",
            month, payment, interest, loan_amt);
        
          }
        }
    
        system("PAUSE");
        return 0;
    }
    Can someone please help me?
    Thanks, I would really appreciate it.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't see where you actually take the payment out of the amount of the loan. (So your loan_amt just goes up and up ...)

  3. #3
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    You never subtract the payment from the loan. Therefore the outstanding loan grows forever.

    EDIT:

    Here's working code:

    Code:
    #include <stdio.h>
    
    int main()
    
    {
        double loan_amt;
        double interest_rate;
        double remaining;
        double interest;
        double payment;
        int month = 1;
       
        printf("Enter the loan amount\n");
        scanf("%lf", &loan_amt);
        printf("Enter the monthly interest rate of the loan\n");
        scanf("%lf", &interest_rate);
        printf("Enter the amount of the monthly payment\n");
        scanf("%lf", &payment);
       
        printf("+-------+---------+-----------+----------+\n");    
        printf("| Month | Payment | Interest  | Remaining | \n");
        printf("+-------+---------+-----------+----------+\n");
       
        interest_rate = (loan_amt*(interest_rate/100));
       
    while(loan_amt > 0)
        { 
          interest = (loan_amt*(interest_rate/100)); 
          /* EDIT START EDIT START EDIT START */
          /* notice the "-payment" at the end, that's the only part i added */
          loan_amt = loan_amt+interest-payment;
          /* EDIT END EDIT END EDIT END */ 
    
          printf("| %3d   | %5.2lf  | %4.2lf     | %.2lf   |\n",
          month, payment, interest, loan_amt);
          month++;
                if(payment > (loan_amt + interest)) 
          {
            interest = (loan_amt*(interest_rate/100));   
            payment = (loan_amt + interest);   
            loan_amt = 0; 
            printf("| %3d   | %5.2lf  | %4.2lf     | %.2lf   |\n",
            month, payment, interest, loan_amt);
        
          }
        }
    
        system("PAUSE");
        return 0;
    }

    EDIT #2:

    You also need to add code to check if the interest is greater than the payment. If that is the case the loop just goes on forever...
    Last edited by ITAmember; 06-24-2009 at 08:01 PM. Reason: adding code

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    5
    Edit* trying to figure it out
    Last edited by beachsidefl321; 06-24-2009 at 08:05 PM. Reason: Didn't see ITAmember's edit

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    5
    Quote Originally Posted by ITAmember View Post

    You also need to add code to check if the interest is greater than the payment. If that is the case the loop just goes on forever...
    I know this sounds stupid but as you can see I have no idea what i'm doing...
    Can you (or anyone?) point me in the right direction as to what code I need to add to stop this loop?

    I know you said I need to add code to check if the interest is > payment but... how do I do that?

  6. #6
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    Code:
          if(interest > payment)
          {
            printf("\nO noes! Your interest is greater than your payment and the \n"
                   "loop goes on forever!\n\n");
            system("PAUSE");
            return 0;
          }

  7. #7
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by ITAmember View Post
    Code:
          if(interest > payment)
          {
            printf("\nO noes! Your interest is greater than your payment and the \n"
                   "loop goes on forever!\n\n");
            system("PAUSE");
            return 0;
          }
    I think

    Code:
    if(interest > payment)
      {
          while (1) {
            printf("\nO noes! Your interest is greater than your payment and the \n"
                      "loop goes on forever!\n\n");
          }
      }
    would be more fitting
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    5
    I'm doing something very wrong.

    using these numbers: loan amount = 5500
    monthly interest rate = 1.2%
    monthly payment = 700

    the table should look like this:

    +--------+-----------+----------+-------------+
    | Month |Payment Interest Remaining
    +--------+-----------+----------+-------------+
    | 1 | 700.00 | 66.00 | 4866.00 |
    | 2 | 700.00 | 58.39 | 4224.39 |
    | 3 | 700.00 | 50.69 | 3575.08 |
    | 4 | 700.00 | 42.90 | 2917.99 |
    | 5 | 700.00 | 35.02 | 2253.00 |
    | 6 | 700.00 | 27.04 | 1580.04 |
    | 7 | 700.00 | 18.96 | 899.00 |
    | 8 | 700.00 | 10.79 | 209.79 |
    | 9 | 700.00 | 2.52 | 0.00 |
    +---------+----------+----------+-------------+

  9. #9
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    Instead of entering 1.2 for the interest rate you enter 0.012. 1.2 would be 120%, 0.012 would be 1.2%.

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    5
    The remaining is suppose to go down, not up!

  11. #11
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    Code:
    Enter the loan amount
    5500
    Enter the monthly interest rate of the loan
    0.012
    Enter the amount of the monthly payment
    700
    +-------+---------+-----------+----------+
    | Month | Payment | Interest  | Remaining |
    +-------+---------+-----------+----------+
    |   1   | 700.00  | 36.30     | 4836.30   |
    |   2   | 700.00  | 31.92     | 4168.22   |
    |   3   | 700.00  | 27.51     | 3495.73   |
    |   4   | 700.00  | 23.07     | 2818.80   |
    |   5   | 700.00  | 18.60     | 2137.41   |
    |   6   | 700.00  | 14.11     | 1451.51   |
    |   7   | 700.00  | 9.58     | 761.09   |
    |   8   | 700.00  | 5.02     | 66.12   |
    |   9   | 66.55  | 0.44     | 0.00   |
    Press any key to continue . . .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Early program stop problem
    By bmb_ksu in forum C Programming
    Replies: 9
    Last Post: 02-19-2006, 09:24 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. help with operator <
    By kashifk in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2003, 03:49 PM