I am trying to make a simple time-calculating program in ASP.NET where people enter an amount, an annual interest, and a payment per month.
I need to get the time in months to when the debt is paid of along with interest, and i want to test if the monthly payment amount is smaller than the monthly interest amount using an if else condition but it doesn’t seem to be working. If I provide a monthly payment smaller than the monthly interest amount, then the program hangs.
My code:
protected void btnCalculate_Click(object sender, EventArgs e)
{
if (IsValid)
{
int BorrowAmount = Convert.ToInt32(txtBorrow.Text);
double InterestRate = Convert.ToDouble(txtRate.Text);
int MonthlyPay = Convert.ToInt32(txtMAmount.Text);
double Rammount = BorrowAmount;
double monthlyIntRate = InterestRate / 12;
//LblNoMonths.Text = Convert.ToString(monthlyIntRate);
//LblNoMonths.Text = Convert.ToString(monthlyIntRate);
double firstmonthlyIntRateAmt = ((monthlyIntRate / 100) * Rammount);
if (MonthlyPay >= firstmonthlyIntRateAmt)
{
int month = 0;
while (Rammount >= MonthlyPay)
{
month++;
double monthlyIntRateAmt = ((monthlyIntRate / 100) * Rammount);
Rammount = Rammount - (MonthlyPay - monthlyIntRateAmt);
}
LblNoMonths.Text = Convert.ToString(month);
}
else
{
LblNoMonths.Text ="Monthly payment is less than the monthly interest rate!!";
}
}
}
The code inside else condition seems to have no effect:
LblNoMonths.Text =@"Monthly payment is less than the monthly interest rate!!";
Am I doing anything wrong?
just press F9 on If Statement (Break Point) and start debugging. to go step forward just use F10 to follow code execution flow! 😉 You can also use VS watch window to see exact amount inside each value … nothing else would help you!