Click below for Step by Step and Interview Q & A (dotnetinter) wrote,
Click below for Step by Step and Interview Q & A

C# interview questions and answers: - Can you write a simple c# code to display Fibonacci series?

These are some strangec# interview questions which are asked in big companies to test your logical thinking ability. Before we move ahead lets first to understand what exactly is Fibonacci series. Fibonacci series are nothing but series of numbers 0,1,1,2,3,5 and so on.

The specialty of this series is that next number is addition of previous two numbers. Below figure explains how Fibonacci series works. We first start with 0 and 1. The next number is addition of “0 + 1” which will give us “1”. The next number will be again addition of previous value which is “1” and the current value which is “1” which will give us “2” and so on.

                                  123 im

Implementing Fibonacci series in c# is a 4 step process. Below image shows the same in a pictorial format.

                                          123 im 2

Step 1:- Define 3 variables “Prev”,”Next” and “Sum”. Initialize these variable with “Prev=1” , “Next = -1” and “Sum=0”.

Step 2:- Add “Prev” and “Next” variable to get the Sum. Display the “Sum” variable.

Step 3:- Swap “Prev” value to “Next” and “Sum” to “Previous”.

Step 4:- Execute “Step 2” and “Step 3” again for the next Fibonacci value.

Below is how the code will look like. Do follow the steps in comment.

123 im 3

The above question is taken from the bestselling c# and .NET interview question book written by Shivprasadkoirala and published by BPB publication.


Tags: .net and c# interview questions, .net and c# training, .net interview questions and answers, csharp interview questions, fibonacci series, information technology questions, programming interview questions

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded