How to print the subsequent nth numbers of the Fibonacci series in C#?

Viewed 333

I know that a similar question may have been asked earlier.

The output of my code is the length of the numbers plus one based on the user input:

i.e. if the user types "5", it is 0, 1, 1, 2, 3, 8.

However, I am trying to print the subsequent nth numbers after the user has provided some number. It should be like this: if the user types "5", then the output should be 5, 8, 13, 21, 34, ...

How should I modify my code to achieve this?

Thank you for your help.

        Console.WriteLine("Please enter any nth number of the Fibonacci series.");

        string user_num = Console.ReadLine();

        int userNumber = int.Parse(user_num);

        int[] FibNum = new int[userNumber];

        int firstNum = 1;
        Console.Write("{0},", firstNum);

        int secondNum = 1;
        Console.Write("{0},", secondNum);

        int sumNum = 0;

        while (sumNum <= userNumber)
        {
            sumNum = (firstNum + secondNum);
            Console.Write("{0}, ", sumNum);

            firstNum = secondNum;
            secondNum = sumNum;               
        }

        Console.ReadLine(); 
1 Answers
Related