Need help in calculating interest rate on loan

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fatimahtaher
    New Member
    • Jan 2007
    • 11

    Need help in calculating interest rate on loan

    Hi,

    I am new to C# programming and my first assignment requires me to calculate total interest paid on a loan. The user will input the loan, the interest rate per month, and the monthly payment. It has to use a while loop and calculate the following:

    Total amount of interest paid and the number of months to pay off the loan.

    I am not a finance major so I have some difficulty with the logistics of how to go about doing this. My code so far is really messy and does not work. Please help!

    Code:
    static void Main(string[] args)
          {
             //declare variables
             double amount_borrowed;
             double interest_rate;
             double monthly_payment;
             double leftover_balance;
             double monthly_interest_payment;
             double adjusted_principle;
             double balance;
             double total_interest_paid;
             double total_months;
             int monthCounter;
    
             
             
             //initialize variables
             amount_borrowed = 0;
             interest_rate = 0;
             monthly_payment = 0;
             adjusted_principle = 0;
             monthly_interest_payment = 0;
             monthCounter = 0;
             total_months = 0;
             leftover_balance = 0;
             total_interest_paid = 0;
             balance = 0;
             adjusted_principle = amount_borrowed - monthly_interest_payment;
             monthly_interest_payment = adjusted_principle * interest_rate;
             leftover_balance = monthly_payment - monthly_interest_payment;
             monthly_interest_payment = (amount_borrowed * interest_rate);
             balance = adjusted_principle - leftover_balance;
             total_interest_paid = monthly_interest_payment;
             
            
    
             //prompt for input
             Console.WriteLine("Enter amount borrowed: ");
             amount_borrowed = Convert.ToDouble(Console.ReadLine());
    
             Console.WriteLine("Enter monthly interest rate: ");
             interest_rate = Convert.ToDouble(Console.ReadLine());
    
             Console.WriteLine("Enter monthly payment: ");
             monthly_payment = Convert.ToDouble(Console.ReadLine());
                               
             //loop until balance is 0
             while (balance > 0)
             {
                //Count the number of months
                monthCounter++;
    
                //Calculate the balance
                balance = adjusted_principle - leftover_balance;
    
                // Calculate the total interest paid
                total_interest_paid = monthly_interest_payment * total_months;
    
                Console.WriteLine("Your new balance: " +  balance);
                balance = Convert.ToDouble(Console.ReadLine());
                
             }//end while
    
             
    
             Console.WriteLine("You paid " + total_interest_paid + " in interest");
             total_interest_paid = Convert.ToDouble(Console.ReadLine());
    
             Console.WriteLine("It took " + total_months + " months to pay off your loan");
             total_months = Convert.ToDouble(Console.ReadLine());
    
    
          }
       }
    }
    Last edited by Ganon11; Jan 26 '07, 03:40 PM. Reason: code tags added
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    I have done some modifications and got it to the point where it will compile.

    this reads the data in and does some (faulty) calculations - code at the end of the program is commented out - fix that when the rest is working
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
          {
             //declare variables
             double amount_borrowed;
             double interest_rate;
             double monthly_payment;
             double leftover_balance;
             double monthly_interest_payment;
             double adjusted_principle;
             double balance;
             double total_interest_paid;
             double total_months;
             int monthCounter;
    
             
             
             //initialize variables
             amount_borrowed = 0;
             interest_rate = 0;
             monthly_payment = 0;
             adjusted_principle = 0;
             monthly_interest_payment = 0;
             monthCounter = 0;
             total_months = 0;
             leftover_balance = 0;
             total_interest_paid = 0;
             balance = 0;       
    
             //prompt for input
             cout << "Enter amount borrowed: ";
             cin >> amount_borrowed;
    
             cout <<"Enter monthly interest rate: ";
             cin >> interest_rate;
    
             cout <<("Enter monthly payment: ");
             cin>>monthly_payment ;
             monthly_interest_payment = adjusted_principle * interest_rate;
             adjusted_principle = amount_borrowed - monthly_interest_payment;
             leftover_balance = monthly_payment - monthly_interest_payment;
             monthly_interest_payment = (amount_borrowed * interest_rate);
             balance = adjusted_principle - leftover_balance;
             total_interest_paid = monthly_interest_payment;
                               
             //loop until balance is 0
             while (balance > 0)
             {
                //Count the number of months
                monthCounter++;
    
                //Calculate the balance
                balance = adjusted_principle - leftover_balance;
    
                // Calculate the total interest paid
                total_interest_paid = monthly_interest_payment * total_months;
    
                cout <<"Your new balance: "<<  balance << endl;
                //balance = Convert.ToDouble(Console.ReadLine());
                
             }//end while
    
           
            // Console.WriteLine("You paid " + total_interest_paid + " in interest");
            // total_interest_paid = Convert.ToDouble(Console.ReadLine());
    
           //  Console.WriteLine("It took " + total_months + " months to pay off your loan");
           // total_months = Convert.ToDouble(Console.ReadLine());
       }
    you need to look at the code for the interest calculations

    Comment

    • fatimahtaher
      New Member
      • Jan 2007
      • 11

      #3
      Originally posted by horace1
      I have done some modifications and got it to the point where it will compile.

      this reads the data in and does some (faulty) calculations - code at the end of the program is commented out - fix that when the rest is working
      Code:
      #include <iostream>
      using namespace std;
      
      int main()
            {
               //declare variables
               double amount_borrowed;
               double interest_rate;
               double monthly_payment;
               double leftover_balance;
               double monthly_interest_payment;
               double adjusted_principle;
               double balance;
               double total_interest_paid;
               double total_months;
               int monthCounter;
      
               
               
               //initialize variables
               amount_borrowed = 0;
               interest_rate = 0;
               monthly_payment = 0;
               adjusted_principle = 0;
               monthly_interest_payment = 0;
               monthCounter = 0;
               total_months = 0;
               leftover_balance = 0;
               total_interest_paid = 0;
               balance = 0;       
      
               //prompt for input
               cout << "Enter amount borrowed: ";
               cin >> amount_borrowed;
      
               cout <<"Enter monthly interest rate: ";
               cin >> interest_rate;
      
               cout <<("Enter monthly payment: ");
               cin>>monthly_payment ;
               monthly_interest_payment = adjusted_principle * interest_rate;
               adjusted_principle = amount_borrowed - monthly_interest_payment;
               leftover_balance = monthly_payment - monthly_interest_payment;
               monthly_interest_payment = (amount_borrowed * interest_rate);
               balance = adjusted_principle - leftover_balance;
               total_interest_paid = monthly_interest_payment;
                                 
               //loop until balance is 0
               while (balance > 0)
               {
                  //Count the number of months
                  monthCounter++;
      
                  //Calculate the balance
                  balance = adjusted_principle - leftover_balance;
      
                  // Calculate the total interest paid
                  total_interest_paid = monthly_interest_payment * total_months;
      
                  cout <<"Your new balance: "<<  balance << endl;
                  //balance = Convert.ToDouble(Console.ReadLine());
                  
               }//end while
      
             
              // Console.WriteLine("You paid " + total_interest_paid + " in interest");
              // total_interest_paid = Convert.ToDouble(Console.ReadLine());
      
             //  Console.WriteLine("It took " + total_months + " months to pay off your loan");
             // total_months = Convert.ToDouble(Console.ReadLine());
         }
      you need to look at the code for the interest calculations

      Thank you. I will try this.

      Fatimah

      Comment

      Working...