Help with this visual c# program

Helios 1 Reputation point
2022-03-25T02:59:21.96+00:00

I'm stuck on this program where I have to create a for loop to predict the population after a given amount of days. Here it is:

Create an application that predicts the approximate size of a population of organisms. The application should use text boxes to allow the user to enter the starting number of organisms, the average daily population increase (as a percentage), and the number of days the organisms will be left to multiply. For example, assume the user enters the following values:

Starting number of organisms: 2
Average daily increase: 30%
Number of days to multiply 10

(Days) (Approximate Population)

1 2
2 2.6
3 3.38
4 4.394
5 5.7122
and so on.

But when I run my code this is all that appears:

(Days) (Approximate Population)
10 0

Here is part the code:

public partial class Population : Form
{
    int count = 1;
    double organisms;
    double dailyIncrease;
    double days;
    double population;

    public Population()
    {
        InitializeComponent();
    }

    private void calculateButton_Click(object sender, EventArgs e)
    {
        try
        {
            organisms = double.Parse(organismsTextBox.Text);

            try
            {
                dailyIncrease = double.Parse(dailyIncreaseTextBox.Text);

                try
                {
                    days = double.Parse(daysTextBox.Text);

                    for (int count = 1; count <= days; count++)

                        population += (population * dailyIncrease / 100);

                    populationListBox.Items.Add(days + "      " + population);

I'm not sure what I'm doing wrong here?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,481 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,306 Reputation points
    2022-03-25T05:19:26.817+00:00

    Hi Helios,
    try following code:

      for (int count = 1; count <= days; count++)
      {
        populationListBox.Items.Add(count + "      " + population);
        population += (population * dailyIncrease / 100);
      }
    
    0 comments No comments

  2. WayneAKing 4,921 Reputation points
    2022-03-25T05:25:44.547+00:00

    First note that you posted with the dotnet-visual-basic
    tag but it is not VB code, it is C#. You should change the
    Forum Tag to dotnet-csharp.

    Try this code in a simple console program to see if it helps
    you focus your ideas for your own code:

    int count = 1;
    double dailyIncrease = 30;
    double days = 10;
    double population = 2;
    
    Console.WriteLine("{0} {1}", count++, population);
    for ( ; count <= days; count++)
    {
        population += (population * dailyIncrease / 100);
        Console.WriteLine("{0} {1}", count, population);
    }
    

    Output:

    1 2
    2 2.6
    3 3.38
    4 4.394
    5 5.7122
    6 7.42586
    7 9.653618
    8 12.5497034
    9 16.31461442
    10 21.208998746

    • Wayne
    0 comments No comments