473,378 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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!

Expand|Select|Wrap|Line Numbers
  1. static void Main(string[] args)
  2.       {
  3.          //declare variables
  4.          double amount_borrowed;
  5.          double interest_rate;
  6.          double monthly_payment;
  7.          double leftover_balance;
  8.          double monthly_interest_payment;
  9.          double adjusted_principle;
  10.          double balance;
  11.          double total_interest_paid;
  12.          double total_months;
  13.          int monthCounter;
  14.  
  15.  
  16.  
  17.          //initialize variables
  18.          amount_borrowed = 0;
  19.          interest_rate = 0;
  20.          monthly_payment = 0;
  21.          adjusted_principle = 0;
  22.          monthly_interest_payment = 0;
  23.          monthCounter = 0;
  24.          total_months = 0;
  25.          leftover_balance = 0;
  26.          total_interest_paid = 0;
  27.          balance = 0;
  28.          adjusted_principle = amount_borrowed - monthly_interest_payment;
  29.          monthly_interest_payment = adjusted_principle * interest_rate;
  30.          leftover_balance = monthly_payment - monthly_interest_payment;
  31.          monthly_interest_payment = (amount_borrowed * interest_rate);
  32.          balance = adjusted_principle - leftover_balance;
  33.          total_interest_paid = monthly_interest_payment;
  34.  
  35.  
  36.  
  37.          //prompt for input
  38.          Console.WriteLine("Enter amount borrowed: ");
  39.          amount_borrowed = Convert.ToDouble(Console.ReadLine());
  40.  
  41.          Console.WriteLine("Enter monthly interest rate: ");
  42.          interest_rate = Convert.ToDouble(Console.ReadLine());
  43.  
  44.          Console.WriteLine("Enter monthly payment: ");
  45.          monthly_payment = Convert.ToDouble(Console.ReadLine());
  46.  
  47.          //loop until balance is 0
  48.          while (balance > 0)
  49.          {
  50.             //Count the number of months
  51.             monthCounter++;
  52.  
  53.             //Calculate the balance
  54.             balance = adjusted_principle - leftover_balance;
  55.  
  56.             // Calculate the total interest paid
  57.             total_interest_paid = monthly_interest_payment * total_months;
  58.  
  59.             Console.WriteLine("Your new balance: " +  balance);
  60.             balance = Convert.ToDouble(Console.ReadLine());
  61.  
  62.          }//end while
  63.  
  64.  
  65.  
  66.          Console.WriteLine("You paid " + total_interest_paid + " in interest");
  67.          total_interest_paid = Convert.ToDouble(Console.ReadLine());
  68.  
  69.          Console.WriteLine("It took " + total_months + " months to pay off your loan");
  70.          total_months = Convert.ToDouble(Console.ReadLine());
  71.  
  72.  
  73.       }
  74.    }
  75. }
Jan 26 '07 #1
2 4303
horace1
1,510 Expert 1GB
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
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5.       {
  6.          //declare variables
  7.          double amount_borrowed;
  8.          double interest_rate;
  9.          double monthly_payment;
  10.          double leftover_balance;
  11.          double monthly_interest_payment;
  12.          double adjusted_principle;
  13.          double balance;
  14.          double total_interest_paid;
  15.          double total_months;
  16.          int monthCounter;
  17.  
  18.  
  19.  
  20.          //initialize variables
  21.          amount_borrowed = 0;
  22.          interest_rate = 0;
  23.          monthly_payment = 0;
  24.          adjusted_principle = 0;
  25.          monthly_interest_payment = 0;
  26.          monthCounter = 0;
  27.          total_months = 0;
  28.          leftover_balance = 0;
  29.          total_interest_paid = 0;
  30.          balance = 0;       
  31.  
  32.          //prompt for input
  33.          cout << "Enter amount borrowed: ";
  34.          cin >> amount_borrowed;
  35.  
  36.          cout <<"Enter monthly interest rate: ";
  37.          cin >> interest_rate;
  38.  
  39.          cout <<("Enter monthly payment: ");
  40.          cin>>monthly_payment ;
  41.          monthly_interest_payment = adjusted_principle * interest_rate;
  42.          adjusted_principle = amount_borrowed - monthly_interest_payment;
  43.          leftover_balance = monthly_payment - monthly_interest_payment;
  44.          monthly_interest_payment = (amount_borrowed * interest_rate);
  45.          balance = adjusted_principle - leftover_balance;
  46.          total_interest_paid = monthly_interest_payment;
  47.  
  48.          //loop until balance is 0
  49.          while (balance > 0)
  50.          {
  51.             //Count the number of months
  52.             monthCounter++;
  53.  
  54.             //Calculate the balance
  55.             balance = adjusted_principle - leftover_balance;
  56.  
  57.             // Calculate the total interest paid
  58.             total_interest_paid = monthly_interest_payment * total_months;
  59.  
  60.             cout <<"Your new balance: "<<  balance << endl;
  61.             //balance = Convert.ToDouble(Console.ReadLine());
  62.  
  63.          }//end while
  64.  
  65.  
  66.         // Console.WriteLine("You paid " + total_interest_paid + " in interest");
  67.         // total_interest_paid = Convert.ToDouble(Console.ReadLine());
  68.  
  69.        //  Console.WriteLine("It took " + total_months + " months to pay off your loan");
  70.        // total_months = Convert.ToDouble(Console.ReadLine());
  71.    }
  72.  
you need to look at the code for the interest calculations
Jan 26 '07 #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
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5.       {
  6.          //declare variables
  7.          double amount_borrowed;
  8.          double interest_rate;
  9.          double monthly_payment;
  10.          double leftover_balance;
  11.          double monthly_interest_payment;
  12.          double adjusted_principle;
  13.          double balance;
  14.          double total_interest_paid;
  15.          double total_months;
  16.          int monthCounter;
  17.  
  18.  
  19.  
  20.          //initialize variables
  21.          amount_borrowed = 0;
  22.          interest_rate = 0;
  23.          monthly_payment = 0;
  24.          adjusted_principle = 0;
  25.          monthly_interest_payment = 0;
  26.          monthCounter = 0;
  27.          total_months = 0;
  28.          leftover_balance = 0;
  29.          total_interest_paid = 0;
  30.          balance = 0;       
  31.  
  32.          //prompt for input
  33.          cout << "Enter amount borrowed: ";
  34.          cin >> amount_borrowed;
  35.  
  36.          cout <<"Enter monthly interest rate: ";
  37.          cin >> interest_rate;
  38.  
  39.          cout <<("Enter monthly payment: ");
  40.          cin>>monthly_payment ;
  41.          monthly_interest_payment = adjusted_principle * interest_rate;
  42.          adjusted_principle = amount_borrowed - monthly_interest_payment;
  43.          leftover_balance = monthly_payment - monthly_interest_payment;
  44.          monthly_interest_payment = (amount_borrowed * interest_rate);
  45.          balance = adjusted_principle - leftover_balance;
  46.          total_interest_paid = monthly_interest_payment;
  47.  
  48.          //loop until balance is 0
  49.          while (balance > 0)
  50.          {
  51.             //Count the number of months
  52.             monthCounter++;
  53.  
  54.             //Calculate the balance
  55.             balance = adjusted_principle - leftover_balance;
  56.  
  57.             // Calculate the total interest paid
  58.             total_interest_paid = monthly_interest_payment * total_months;
  59.  
  60.             cout <<"Your new balance: "<<  balance << endl;
  61.             //balance = Convert.ToDouble(Console.ReadLine());
  62.  
  63.          }//end while
  64.  
  65.  
  66.         // Console.WriteLine("You paid " + total_interest_paid + " in interest");
  67.         // total_interest_paid = Convert.ToDouble(Console.ReadLine());
  68.  
  69.        //  Console.WriteLine("It took " + total_months + " months to pay off your loan");
  70.        // total_months = Convert.ToDouble(Console.ReadLine());
  71.    }
  72.  
you need to look at the code for the interest calculations

Thank you. I will try this.

Fatimah
Jan 26 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jason | last post by:
I need help designing a query (or two) in MS Access that will calculate a compounded rate of interest. This would be no problem using a simple FV function if the Interest Rate were constant....
0
by: redmondtab | last post by:
I have done this . Write a C program that will accept keyboard input and display a loan amortization schedule back on the screen. The program should ask for the Loan Amount (real number), Term...
2
by: lab3terch | last post by:
I am working on a program in which the user enters an amount of money in a textbox, selects a beginning and ending date using the datetimepicker, and then the program calculates the new amount based...
1
by: placid | last post by:
Hi all, I want be able to work the download rate for downloading HTML pages using the urllib.urlopen(url). The way i thought i would do this is start a timer (or log the current time) just...
1
by: lenin42001 | last post by:
Please can you help Have tried various means but cannot get this to work. The saver is prompted for the amount they wish to invest & a time period(in years) If initial investment is over £1000 & if...
4
by: bc070200263 | last post by:
This is an Islamic law that Muslims should pay their Zakat with the rate of 2.5%, means in Rs 100 they should pay Rs 2.5 as Zakat amount. Write a simple program which asks from the user The total...
14
Dököll
by: Dököll | last post by:
Greetings and salutations! Hope it's a good week thus far... Got myself thinking and wanted to see what can be achieved with a certain bit of code: public double getPayment( ) { ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.