Question
implement a do-while loop to this solution below in C# language only private void btnCalculate_Click(object sender, EventArgs e) { //clear the items in the list
implement a do-while loop to this solution below in C# language only
private void btnCalculate_Click(object sender, EventArgs e) { //clear the items in the list box lstResult.Items.Clear(); //variable declarations double currentPopulation; int numberOfYears; double annualGrowthRate; //get the current population, if not valid display an error message if (Double.TryParse(txtCurrentPopulation.Text, out currentPopulation)) { //get the number of year, if not valid display an error message if (int.TryParse(txtNumberOfYears.Text, out numberOfYears)) { //get the annual growth rate, if not valid display an error message if (double.TryParse(txtAnnualGrowthRate.Text, out annualGrowthRate)) { //display the header for output lstResult.Items.Add("Year\tPopulation"); //get the current year int year = DateTime.Today.Year; //display the population in current year lstResult.Items.Add(year + "\t" + currentPopulation.ToString("0.00")); //run a loop for the number of years given for (int i = 0; i < numberOfYears; i++) { //compute the population currentPopulation = currentPopulation + (currentPopulation * annualGrowthRate / 100); //increment year year++; //display the population for each year lstResult.Items.Add(year + "\t" + currentPopulation.ToString("0.00")); } } //display error message if annual growth rate entered is invalid else { MessageBox.Show("Invalid input for annual growth rate."); txtAnnualGrowthRate.Focus(); } } //display error message if number of years entered is invalid else { MessageBox.Show("Invalid input for number of years."); txtNumberOfYears.Focus(); } } //display error message if current population entered is invalid else { MessageBox.Show("Invalid input for current population."); txtCurrentPopulation.Focus(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started