0

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;
            }
        }
Lakkes
  • 25
  • 5
  • It alwasy returns 2 with what Input? Does the parsing work? Is there any exception thrown? – Christopher Nov 18 '19 at 19:05
  • First you set sum. The you output sum. Then you shift half the numbers forward. Then you override sum. Nothing in there makes any sense. Please add some comment so we (and you) can figure out your intent. – Christopher Nov 18 '19 at 19:06

1 Answers1

1

You were setting num1 to sum not sum to num2.

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

            lblErg.Text = Convert.ToString(sum);
Hogan
  • 69,564
  • 10
  • 76
  • 117