# Fibonacci series program

In this post, you will learn how to write Fibonacci series program in different programming languages.

The Fibonacci series are the sequence of numbers in which the next number is the sum of the previous two numbers. The Fibonacci series was well-known hundreds of years earlier. The "**Fibonacci**" name came from the nickname "**Bonacci**".

We can easily remember Fibonacci Sequence using the Fibonacci Day, which is November 23rd. As 23rd November has the digits "1, 1, 2, 3" which is part of the sequence.

In the above image, the first two numbers are 0 and 1. So, according to the Fibonacci rule the third number is 1 (sum of 0 and 1). The fourth number is 2 and so on.

```
0 + 1 = 1 // 0, 1, 1
1 + 1 = 2 // 0, 1, 1, 2
1 + 2 = 3 // 0, 1, 1, 2, 3
2 + 3 = 5 // 0, 1, 1, 2, 3, 5
```

0 ,1 , 1, 2, 3, 5, 8, 13, 21, 34.... ## Fibonacci Series Program in PHP using For Loop

Here, you will learn how to print Fibonacci series using a for loop in PHP. In this below code, **$f1** contains first number, i.e., **0** and **$f2** contains second number, i.e., **1** and **$n** contains total Fibonacci series number count.

```
<?php
$f1 = 0;
$f2 = 1;
$n = 15;
echo $f1;
echo '<br/>';
echo $f2;
for($i = 1; $i <= $n; $i++) {
$f3 = $f1 + $f2;
$f1 = $f2;
$f2 = $f3;
echo $f3 ."<br />";
}
?>
```

**Output of the above code:**

```
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
```

## Fibonacci series program in Java using while loop

Here, you will learn how to print Fibonacci series using while loop in Java. In this below code, **n1** contains first number, i.e., 0 and **n2** contains second number, i.e., **1** and **n** contains total Fibonacci series number count. In each iteration of the while loop, we get the next number by adding the previous two numbers and simultaneously we swap the first number with the second and the second with the third.

```
public class FibonacciSeries{
static void fibonacciSeries(int n)
{
int n1 = 0, n2 = 1;
int counter = 0;
while (counter < n) {
// Print the number
System.out.print(n1 + " ");
int n3 = n2 + n1;
n1 = n2;
n2 = n3;
counter = counter + 1;
}
}
// Driver Code
public static void main(String args[])
{
// initialize number n
int n = 13;
// Call function
fibonacciSeries(n);
}
}
```

**Output of the above code:**

`0 1 1 2 3 5 8 13 21 34 55 89 144 `

## Fibonacci series in Python

In the given Python program, we have asked the user to enter the number of series and then used the while loop to print the Fibonacci series sequence.

```
series = int(input("How many number of series? "))
x1, x2 = 0, 1
count = 0
if series <= 0:
("Invalid: Please enter a positive integer")
elif series == 1:
print("Fibonacci series upto ",series,":")
print(x1)
else:
print("Fibonacci series sequence:")
while count < series:
print(x1)
xth = x1 + x2
x1 = x2
x2 = xth
count += 1
```

**Output of the above code:**

```
How many number of series? 12
Fibonacci series sequence:
0
1
1
2
3
5
8
13
21
34
55
89
```

## Fibonacci Series Program in C using Recursive Function

Recursion function is a function which is called by itself. In the given example, we call the recursion function to get the Fibonacci series. A recursion function continues until some condition is met to prevent it. That's why we use the if statement to break the infinite recursion.

In the given program, we use recursion to generate the fibonacci series. The function **fibonacciSeries()** is called recursively until we get the result. In the function, we first check if the number **n** is greater than 0. If yes, we recursively call **fibonacciSeries()** with the values **n-1** and **n-2**.

```
// C program to print Fibonacci series
#include<stdio.h>
// Recursive function
void fibonacciSeries(int n){
static int n1=0,n2=1,n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d ",n3);
fibonacciSeries(n-1);
}
}
int main(){
int n;
printf("How many number of series? : ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
//Call Function
fibonacciSeries(n-2);
return 0;
}
```

**Output of the above code:**

```
How many number of series? : 15
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
```

## Fibonacci Series Program in C++ using recursion

Here is the Fibonacci series program in C++ using recursion.

```
#include <iostream>
using namespace std;
int fib(int x) {
if((x==1)||(x==0)) {
return(x);
}else {
return(fib(x-1)+fib(x-2));
}
}
int main() {
int x , i=0;
cout << "Enter the number of terms of series : ";
cin >> x;
cout << "\nFibonnaci Series : ";
while(i < x) {
cout << " " << fib(i);
i++;
}
return 0;
}
```

**Output of the above code:**

```
Enter the number of terms of series : 13
Fibonnaci Series : 0 1 1 2 3 5 8 13 21 34 55 89 144
```

### Related Articles

**Java program to find area of triangle**

Area of circle program in Java

Remove duplicate elements from array in Java

Capitalize first letter of each word Java

Convert binary to decimal in Java

Convert array to list Python

Python take screenshot of specific window

Web scraping Python BeautifulSoup

Check if two strings are anagrams Python

Python program to add two numbers

Print new line python

Prime factors of a number in c

Armstrong number program in c

Write a program to check leap year in c

C program to find area of rectangle

C program to convert celsius to fahrenheit

Fibonacci series program in C using recursion

Write a program to find area of circle in C

C program to convert Decimal to Octal

C program to convert decimal to binary

C program to check whether a number is even or odd

Area of circle program in Java

Remove duplicate elements from array in Java

Capitalize first letter of each word Java

Convert binary to decimal in Java

Convert array to list Python

Python take screenshot of specific window

Web scraping Python BeautifulSoup

Check if two strings are anagrams Python

Python program to add two numbers

Print new line python

Prime factors of a number in c

Armstrong number program in c

Write a program to check leap year in c

C program to find area of rectangle

C program to convert celsius to fahrenheit

Fibonacci series program in C using recursion

Write a program to find area of circle in C

C program to convert Decimal to Octal

C program to convert decimal to binary

C program to check whether a number is even or odd