Lakkes
Lakkes

Reputation: 25

I can't figure out why my fibonacci sequence isn't working

I have to make a little program to calculate a certain iteration of the fibonacci sequence. right now, it always gives out a 2, no matter what i do...

public partial class Form1 : Form
       {
        int num1=0;
        int num2=1;
        int sum=0;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnCal_Click(object sender, EventArgs e)
        {
            for (int z = 0; z <= Convert.ToInt32(txtCal.Text); z++)
            {        
                sum = num1 + num2;

                lblErg.Text = Convert.ToString(sum);
                num1 = num2;
                sum = num1;
            }
        }

Upvotes: 0

Views: 67

Answers (1)

Hogan
Hogan

Reputation: 70531

You were setting num1 to sum not sum to num2.

            sum = num1 + num2;
            num1 = num2;
            num2 = sum;

            lblErg.Text = Convert.ToString(sum);

Upvotes: 1

Related Questions