This is for Visual Studios 2015 Create a form using While and For loop with text
ID: 3803931 • Letter: T
Question
This is for Visual Studios 2015
Create a form using While and For loop with textbox to enter loan amount, term of the loan in year, starting annual rate, ending annual rate and rate step value, then use the Visual Basic’s Pmt function to compute the payment for each rate and display the monthly payments in a listbox.
I've gotten this far for using "while" and when entering in the information, I get the same payment. There is something wrong with the calculations and I do not know what i'm missing.
double loan, term, startRate, endRate, stepValue, payment, rate;
loan = double.Parse(textBox1.Text);
term = double.Parse(textBox2.Text);
startRate = double.Parse(textBox3.Text);
endRate = double.Parse(textBox4.Text);
stepValue = double.Parse(textBox5.Text);
rate = startRate;
listBox1.Items.Clear();
while(rate<=endRate)
{
payment = Financial.Pmt((startRate / 100) / 12,term*12,-loan);
listBox1.Items.Add("Rate="+rate.ToString("p") + " Payment is: "
+ payment.ToString("c"));
rate += stepValue;
I also need to know the "For" loop as well.
Explanation / Answer
Hahaha... You made a silly mistake. I think you need to calm down a bit. Look into the line where you called Financial.pmt(....), You see it? You stated the rate as startRate, but startRate is fixed throughout the method, so you are getting the same value. You should change it to rate, that changes throughout the method to get dynamic values.
<CODE>
double loan, term, startRate, endRate, stepValue, payment, rate;
loan = double.Parse(textBox1.Text);
term = double.Parse(textBox2.Text);
startRate = double.Parse(textBox3.Text);
endRate = double.Parse(textBox4.Text);
stepValue = double.Parse(textBox5.Text);
rate = startRate;
listBox1.Items.Clear();
while(rate<=endRate) {
payment = Financial.Pmt((rate / 100) / 12,term*12,-loan);
listBox1.Items.Add("Rate="+rate.ToString("p") + " Payment is: "
+ payment.ToString("c"));
rate += stepValue;
}
I hope you now know what mistake you were doing :(
For loop too can be implemented in similar fashion. syntax of for loop if for(<initialize>,<condition>;<update>)
so by rewriting the code we have:
double loan, term, startRate, endRate, stepValue, payment, rate;
loan = double.Parse(textBox1.Text);
term = double.Parse(textBox2.Text);
startRate = double.Parse(textBox3.Text);
endRate = double.Parse(textBox4.Text);
stepValue = double.Parse(textBox5.Text);
rate = startRate;
listBox1.Items.Clear();
for (; rate < endRate; rate = rate + stepValue) // You can see that the first value is left blank,
//since we have already initialized the value of rate
{
payment = Financial.Pmt(rate / 100) / 12,term*12,-loan);
listBox1.Items.Add("Rate="+rate.ToString("p") + " Payment is: "
+ payment.ToString("c"));
}
I hope these answers will help you. Don't forget to comment back if you need any more explanations.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.