-1

wrote a program to calculate and display the first 20 fibonacci numbers, the sequence goes as follows:

1, 1, 2, 3, 5, 8, 13... (each number is the sum of the previous two numbers)

The problem is that the numbers that get displayed are from 2 onwards , the first and second numbers of the sequence do not get displayed , could someone tell me what needs to be done to correct this?

Code:

        private void button1_Click(object sender, EventArgs e)
    {
        int previousNumber = 1;
        int currentNumber = 1;
        int nextNumber = 1;

        while (currentNumber <= 11000)
        {
            nextNumber = previousNumber + currentNumber;
            previousNumber = currentNumber;
            currentNumber = nextNumber;
            textBox1.AppendText(Convert.ToString(nextNumber) + " ");
                nextNumber++;

        }
    }
3
  • 2
    Don't get me wrong but I think in this case you can learn much more if you just set a breakpoint anywhere use the debugger and step through your programm - it's not that hard to see what is going on and you should develop a sense for this kind of reasoning.
    – Random Dev
    Commented Aug 31, 2014 at 19:54
  • while doing so you should watch your 3 variables and then think about some of the lines (for example: do you really need the nextNumber++?, do you really need the nextNumber variable?)
    – Random Dev
    Commented Aug 31, 2014 at 19:56
  • Why not just print the previousNumber and currentNumber before the while loop?
    – Dmitry
    Commented Aug 31, 2014 at 20:00

4 Answers 4

0

I suggest carefully tracing through the logic and predicting what the computer will do at each step. Since the bug affects the very first output, you won't have to look at very many statements to encounter the problem. This is a basic skill for a programmer, so it will be well worth the time, especially since this sounds like homework.

0
0

Initially you need to display the current number twice before next number calculation. Also you need to move the number display before the next number calculation. Also next number should be previous number + currentNumber. I have made the changes to your code below which should work.

private void button1_Click(object sender, EventArgs e)
{
    int previousNumber = 1;
    int currentNumber = 1;
    int nextNumber = 1;

    textBox1.AppendText(Convert.ToString(currentNumber) + " ");
    while (currentNumber <= 11000)
    {
        textBox1.AppendText(Convert.ToString(currentNumber) + " ");
        nextNumber = previousNumber + currentNumber;
        previousNumber = currentNumber;
        currentNumber = nextNumber;

            nextNumber = previousnNumber + currentNumber;

    }
}
0

Since the first two digits in a Fibonacci are the seed values (1 and 1 or 0 and 1) you should print those first and then calculate the next values.

I would simplify the code. You can test it out at https://dotnetfiddle.net/3cV96L

int initialSeed = 1;
int currentNumber = 1;

//Write seed values
Console.Write("{0} {1} ", initialSeed, currentNumber);

while (currentNumber <= 11000)
{
    currentNumber += currentNumber;
    Console.Write(currentNumber + " ");
}
0

Just change int currentNumber = 0;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.