**Fibonacci series program is **program which is used to print Fibonacci seines till given number.

Lets see what is **Fibonacci series , Fibonacci series algorithm, Fibonacci series in java.**

**What is Fibonacci series?**

Fibonacci is a series of numbers starting from 0 and adding with two numbers before it.

**Fibonacci series logic**

Logic is : Fibonacci series is a collection or set of the numbers starting with zero or one, followed by the sum of two preceding numbers.

consider Fibonacci series till **N** which is implemented as **0,1,(0+1),(0+2)…**. N.

**Example :**

Write Fibonacci series up to 8 numbers.

0,1,1,2,3,5,8,13.

we calculated this series as first two number as 0 and 1 , 0+1 => 1, 1+2=>3 ,3+2=>5,5+3=>8,8+5=>13

**Fibonacci series Algorithm **

- Start
- Declare variables
- Take N from user
- Using for loop print sries
- End

**Fibonacci series using for Loop **

This is the one method to print the Fibonacci series.

In this, we use the for loop to iterate number to range up to which we want to print the Fibonacci series.

## Fibonacci series program

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.*; public class Main { public static void main(String[] args) { int i, n, t1 = 0, t2 = 1, nextTerm; Scanner sc=new Scanner(System.in); System.out.println("Enter the number of terms:"); n=sc.nextInt(); System.out.println("Fibonacci Series:"); for (i = 1; i <= n; ++i) { System.out.print(t1+", "); nextTerm = t1 + t2; t1 = t2; t2 = nextTerm; } } } |

**Output :**

**Explanation :**

**1. Execution of the program starts with the main function.**

**2. Declare variables**

i, n, t1, t2 ,1, nextTerm

**3. Take N value that is how many series user want**

n=sc.nextInt();

=> n= 3

**4. Now the main logic begins**

for (i = 1; i <= n; ++i) => we write for loop to print series

**At iteration 1**

i=1, 1<=3

System.out.print(t1+”, “); => t1 = 0 printed

nextTerm = t1 + t2; =>nextTerm = 1

t1 = t2; => t1= 1

t2 = nextTerm; => t2=1

**At iteration 2**

i=2, 2<=3

System.out.print(t1+”, “); => t1 = 1 printed

nextTerm = t1 + t2; =>nextTerm = 2

t1 = t2; => t1= 1

t2 = nextTerm; => t2=2

**At iteration 3**

i=3, 3<=3

System.out.print(t1+”, “); => t1 = 1 printed

nextTerm = t1 + t2; =>nextTerm = 3

t1 = t2; => t1= 2

t2 = nextTerm; => t2=3

**At iteration 4**

i=4, 4<=3 condition false

**5. End with the program.**

## Leave a Reply