Umar Anjum

Umar Anjum

  • NA
  • 43
  • 3.5k

Need help with mortgage calculator

Jan 30 2014 8:30 PM
Sorry if this has been posted before but I searched the forum and couldn't find a solution. Basically, I need to create a mortgage calculator in C# that allows the user to input the mortgage amount, the interest rate and the duration of the mortgage, the program has to contain a button which, when pressed, calculates the interest the user has to pay, calculates the total amount the user has to pay and the monthly repayments. Below is a copy of the code I've written so far, I was told to use an integer instead of a float/double by my teacher, who said its more efficient. However, there's an error, as when i run the program and type in all of the text boxes and click on the button the outputs are messed up. If someone could take a look at the code and see what I've done wrong and correct it for me I would really appreciate it. Thanks


        public int Amount;
        public int InterestRate;
        public int LoanDuration;
        public int InterestToBePaid;
        public int TotalAmountToBePaid;
        public int MonthlyRepayments;

        public frmMortgageCalculator()
        {
            InitializeComponent();
        }

        private void tabpSimpleInterest_Click(object sender, EventArgs e)
        {

        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                Amount = int.Parse(txtAmount.Text);
                InterestRate = int.Parse(txtInterestRate.Text);
                LoanDuration = int.Parse(txtDuration.Text);
                //Calculates the interest
                InterestToBePaid = InterestRate / 100 * Amount;
                lblInterestToBePaid.Text = "The Total Interest You Have To Pay Is:" + InterestToBePaid;

                // Calculates the total amount
                TotalAmountToBePaid = Amount + InterestRate;
                lblTotalAmountToBePaid.Text = "The Total Amount You Have To Pay Is:" + TotalAmountToBePaid;

                //Calculates the monthly repayments 
                MonthlyRepayments = Amount + InterestRate / LoanDuration;
                lblMonthlyRepayments.Text = "Your Monthly Repayments Are:" + MonthlyRepayments;

            }

            catch(Exception exc)
            {
                MessageBox.Show(exc.Message);
            }

Answers (10)