Reputation: 11
the tuition for a full-time student is $12000 per year. It has been announced that the tuition will increase by 2% each year for the next 5 years.How can i write a c# loop to calculate Tution increase by 2% each year for the next 5 years
My code thus far is..
private void button1_Click(object sender, EventArgs e)
{
string display;
double initialfee = 12000.00;
double increase,newfee;
double rate = 0.02;
listBox1.Items.Clear();
for (int year = 1; year <= 5; year++)
{
increase = initialfee * rate * year;
newfee = increase + initialfee;
display = "year " + year.ToString() + ": " + " Amount " + "$" + newfee;
listBox1.Items.Add(display);
Upvotes: 1
Views: 1425
Reputation: 31
Here is one solution to calculate.
private void button1_Click(object sender, EventArgs e)
{
string display;
double initialfee = 12000.00;
double increase,newfee;
double rate = 0.02;
listBox1.Items.Clear();
for (int year = 1; year <= 5; year++)
{
newfee = initialfee + (initialfee * 2/100 * year);
display = "year " + year.ToString() + ": " + " Amount " + "$" + newfee;
}
}
this calculates like 2% first year, 4% next year and so on.
if 2% Compound increase each year is what is required, then,
private void button1_Click(object sender, EventArgs e)
{
string display;
double initialfee = 12000.00;
double increase,newfee;
double rate = 0.02;
listBox1.Items.Clear();
for (int year = 1; year <= 5; year++)
{
if(year == 1)
newfee = initialfee;
else
newfee = newfee + (newfee * 2 / 100);
display = "year " + year.ToString() + ": " + " Amount " + "$" + newfee;
}
}
hope this helps!
Upvotes: 2
Reputation: 1192
You dont need to multiply by year. Try this
string display;
double initialfee = 12000.00;
double increase=0,newfee;
double rate = 0.02;
for (int year = 1; year <= 5; year++)
{
if(year>1)
{
increase = initialfee * rate;
}
initialfee = increase + initialfee;
display = "year " + year.ToString() + ": " + " Amount " + "$" + initialfee;
Console.WriteLine(display);
}
Output:
year 1: Amount $12000
year 2: Amount $12240
year 3: Amount $12484.8
year 4: Amount $12734.496
year 5: Amount $12989.18592
Upvotes: 2
Reputation: 186803
Linq solution:
using System.Linq;
...
// Please, see separation:
// Model: here we define condition
// initial conditions
double initialfee = 12000.00;
double rate = 0.02;
int years = 5;
// Business logic: here we obtain data
// please, notice formatting (here is C# 6.0 string interpolation)
// which is easier to read and maintain then contructing string from chunks added.
var data = Enumerable
.Range(1, years)
.Select(year => $"Year {year}: Amount: {Math.Pow(1.0 + rate, year - 1) * initialfee:f2}")
.ToArray();
// Representation: UI representation
// Add items in one go to prevent redrawing and blinking
listBox1.Items.AddRange(data);
Outcome is
Year 1: Amount: 12000.00
Year 2: Amount: 12240.00
Year 3: Amount: 12484.80
Year 4: Amount: 12734.50
Year 5: Amount: 12989.19
I know, you're looking for a loop solution, but in Real World we usually prefer working with data queries.
Upvotes: 0