# Fibonacci Series in C, Java and C#

## What is Fibonacci?

Fibonacci (also known as Leonardo of Pisa, Leonardo Pisano, Leonardo Bonacci, and Leonardo Fibonacci) is 13th century Italian mathematician. He is known to our world because of number sequence which he used in his book and fibonacci series was one of them. You can know more about Fibonacci on Wikipedia.

## What is Fibonacci Numbers?

In math world, Fibonacci series is the number sequence in which each number is the sum of the two previous numbers. The sequence begins with 0 and 1. The next number would be sum of 0 & 1. This process will continue till nth number.

From above table you can see that value in the 2nd location is the sum of previous two values (i.e. values present in location 0 and 1). Similarly value of 6th location is sum of value of 4th and 5th. This is how Fibonacci numbers can be generated. I hope logic for the Fibonacci number is clear and we can move to example for better understandings. Below example will give you Fibonacci number of the given index or location, you can refer above image for the same.

## Find nth fibonacci number

For your convenience I have created one function/method to generate Fibonacci number series. You can call this function/method and pass position or index of Fibonacci number you want to get.

Let’s delve into above method/function. If you pass 0 or 1 as index to above method/function then it will return the same value. If you pass greater than 2 then it will execute the for loop block to calculate the fibonacci number of that place. Inside for loop, we are calculating sum of last two numbers and swapping that value. But for me interesting point is loop condition, it’s “i < index -1”. So if you pass 4 as index (i.e. 0 to 4, 5 times loop) it will loop for 3 times, why?

Because we are dealing with 2 values of fibonacci series number in one iteration of for loop like I showed you in above block. So we have to loop only “i <= index – 2” or “i < index – 1”.

## Nth Fibonacci numbers in C

Note: I think you can easily modify this code for C++. If you find any difficulty then please let me know, I will provide C++ code.

Scroll to Continue

## First N Fibonacci numbers

Let’s have one more example where we will generate first n Fibonacci numbers. For example, first 10 or 5 fibonacci numbers. For this purpose we will reuse the above GenerateFibbonacciSeries method or function in our program. IF you compare this program with first one then you will find only one difference in this program. In “Main()” method we have one for loop which loops depending on value entered by user. In loop body we are passing value of i to GenerateFibbonacciSeries method or function and showing the value returned by method or function. Logically it will pass values from 0 to index and will show the output.

## Fibonacci series using recursion

In this section we will generate Fibonacci number using recursion; if you are new to recursion then you can learn more about it here. To incorporate recursion logic we need to modify GenerateFibbonacciSeries method or function. But don’t worry it’s going to be very minor changes.

## Recursive Fibonacci in Java

You can modify above code to generate Fibonacci number up to N numbers using recursion. I hope this tutorial helped to learn the concept of Fibonacci number series. Thank you, keep visiting.