How do I get this for loop to display double the number of pennies every day?
private void btnCalculate_Click(object sender, EventArgs e)
{
int days = Int32.Parse(txtNumberOfDays.Text);
int totalPay = 0;
int pennies = 1;
for (int i = 0; i < days; i )
{
totalPay = pennies;
pennies = pennies * 2;
}
txtTotalPennies.Text = totalPay.ToString();
}
The code below is my first attempt at this problem and I think it works correctly. Can you tell me if this code is correct?
private void btnCalculate_Click(object sender, EventArgs e)
{
Double NumberOfDays = Convert.ToDouble(txtNumberOfDays.Text);
Double TotalPennies = 1;
for (Double i = 1; i <= NumberOfDays; i )
{
TotalPennies = Math.Pow(2, NumberOfDays - 1);
}
txtTotalPennies.Text = TotalPennies.ToString();
}
EDITED - Is the below code correct?
private void btnCalculate_Click(object sender, EventArgs e)
{
int days = Convert.ToInt32(txtNumberOfDays.Text);
int totalPay = 0;
int pennies = 1;
for (int i = 0; i < days; i )
{
totalPay = pennies;
pennies = pennies * 2;
}
txtTotalPennies.Text = totalPay.ToString();
}
CodePudding user response:
you need to convert string to int
Convert.ToInt32(string str);
so
private void btnCalculate_Click(object sender, EventArgs e)
{
int days = Convert.ToInt32(txtNumberOfDays.Text);
int totalPay = 0;
int pennies = 1;
for (int i = 0; i < days; i )
{
totalPay = pennies;
pennies = pennies * 2;
}
txtTotalPennies.Text = totalPay.ToString();
}
CodePudding user response:
private void btnCalculate_Click(object sender, EventArgs e)
{
int days = Convert.ToInt32(txtNumberOfDays.Text);
int totalPay = 0;
int pennies = 1;
for (int i = 0; i < days; i )
{
totalPay = pennies;
pennies = pennies * 2;
}
txtTotalPennies.Text = totalPay.ToString();
}
