By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,087 Members | 1,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,087 IT Pros & Developers. It's quick & easy.

help with calculate annual interest windows app

P: 1
Hey guys I have no idea with what im doing with loops( a for, do, or while loop any of the 3) or the logic on how to do this and i have to have this done for school tommorow. I have to create a windows app with 4 text boxes for starting balance, number of years, interest rate %, and what you will have which is a read only. One calculate button to do this all.

heres what i have now all and any help is much appreciated thanks because i really dont want to fail..

namespace WindowsApplication1
{
public partial class Form1 : Form
{
double openingBalance;
double years;
double interestRate;
double final;




public Form1()
{
InitializeComponent();
}

private void btnGo_Click(object sender, EventArgs e)
{
openingBalance = Convert.ToString(txtBalance.Text);
years = Convert.ToString(txtYears.Text);
interestRate = 1 + Convert.ToString(txtRate.Text) / 100;
final = Convert.ToString(txtFinal.Text);

int final = 0;
do
{
openingBalance * interestRate;
++final;

} while ();

}

private void txtYears_TextChanged(object sender, EventArgs e)
{


}

}
Oct 12 '06 #1
Share this Question
Share on Google+
2 Replies


Banfa
Expert Mod 5K+
P: 8,916
Expand|Select|Wrap|Line Numbers
  1. int final = 0;
  2.  
  3. do
  4. {
  5.     openingBalance * interestRate;
  6.     ++final;
  7. } while ();
  8.  
You virtuall have it, all you need is the end condition in the while loop. And what is the end condition? Well what are you counting? Years that's what, you need to add years Years interest. You have a counter going in final so all you need to do is compare final to years to stop the loop when it has been exectued years times.

This is basically

Expand|Select|Wrap|Line Numbers
  1. int final = 0;
  2.  
  3. do
  4. {
  5.     openingBalance * interestRate;
  6.     ++final;
  7. } while (final <= years);
  8.  
Not it is <= because the value is tested at the end of the loop after the increment.

However I think most C programmers would use a for loop for this. The loop 1 time for every instance is one of the most common loops (because you are often looping once for each entry in an array).

This loop has the form

Expand|Select|Wrap|Line Numbers
  1. int loop;
  2. int number_of_times_to_loop = <SomeValue>;
  3.  
  4. for( loop=0; loop<number_of_times_to_loop, loop++)
  5. {
  6.    // code to be executed here
  7. }
  8.  
Note here we use < because the loop end test is at the start of the loop before the increment.

I would only use a do ... while loop if a really needed the loop body to execute before the first test of the end condition.
Oct 12 '06 #2

100+
P: 293
D_C
Expand|Select|Wrap|Line Numbers
  1. openingBalance * interestRate;
Might want to be changed to
Expand|Select|Wrap|Line Numbers
  1.  openingBalance *= interestRate;
That actually stores the result ( openingBalance = openingBalance * interestRate).
Oct 13 '06 #3

Post your reply

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