Printing desired Fibonacci Series in Java (2 Methods)

DESCRIPTION

First we must know what Fibonacci series is.

0 1 1 2 3 5 8 13 21………..

By definition, the first Fibonacci number is ‘0’ and second Fibonacci number is ‘1’. Then each subsequent number is calculated based on sum of the previous two. Numbers of Fibonacci sequence are known as Fibonacci numbers.

This sequence has many applications in mathematics and Computer Science.

Here in the program we have two initial values of series, ‘0’ and ‘1’ in variables ‘first’ and ‘sec’ respectfully.

Then user enters the Fibonacci number in variable ‘n‘.

When n>2, the loop is executed, its value starts from 3 as two values are printed already.

In loop the sum of previous two numbers are stored in variable ‘sum’ then value of ‘sec’ is saved in ‘first’ and value of ‘sum’ is saved in ‘sec’, at last the value of sum is printed. Further the loop is continued until the value of ‘i’ reaches to the value of ‘n’.

DESCRIPTION

First we must know what Fibonacci series is.

0 1 1 2 3 5 8 13 21………..

By definition, the first Fibonacci number is ‘0’ and second Fibonacci number is ‘1’. Then each subsequent number is calculated based on sum of the previous two. Numbers of Fibonacci sequence are known as Fibonacci numbers.

This sequence has many applications in mathematics and Computer Science.

Here in the program we have two initial values of series, ‘0’ and ‘1’ in variables ‘first’ and ‘sec’ respectfully.

Then user enters the Fibonacci number in variable ‘n‘.

Here the loop starts from value ‘1’ and ends when its value reaches to the value equal to ‘n’.

In loop, the value of first and sec are added and stored in variable first, then  value of first and sec are subtracted and stored in variable sec. At last the value of sec is printed.

SOURCE CODE
SOURCE CODE

Author Description

humanz

Our mission is to bring the affordable Technology education to people everywhere, in order to help them achieve their dreams and change the world. - Follow @program_guide

No comments yet.

Join the Conversation