Program to print Fibonacci serires

Hi all , Today I am going to write a post on popular interview program which is Write a program in java to print Fibonacci series .
So what is Fibonacci series
In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in the following integer sequence:
 1 1 2 3 5 8 13 21 34 55 89 144 ...
or (often, in modern usage):
0 1 1 2 3 5 8 13 21 34 55 89 144 ....

The Fibonacci spiral: an approximation of the golden spiral created by drawing circular arcs connecting the opposite corners of squares in the Fibonacci tiling  this one uses squares of sizes 1, 1, 2, 3, 5, 8, 13, 21, and 34.
By definition, the first two numbers in the Fibonacci sequence are 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.
So here is very simple method to print Fibonacci series of first 20 numbers :




 public class fibonacci {
         
            public static void main(String[] args) {
                        int first=0;
                        int second =1;
                        int element =0;
                        System.out.print(first);
                        System.out.print(second);
                        for (int i = 0; i < 20; i++) {
                                    element=first+second;
                                    System.out.print(" "+element);
                                 
                                    first=second;
                                    second=element;
                        }
            }
         

}



Output is :



0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946



Now I am going to show how to print Fibonacci series  with user input . For example if user enters 3 then first three numbers should be printed . if users inputs 5 then first first 5 numbers of the Fibonacci series will be printed .
So The program below is print Fibonacci series  with user input in java :




import java.util.Scanner;


public class fibonacci {
         
            public static void main(String[] args) {
                     
                        Scanner in = new Scanner(System.in);
                        System.out.println("please enter length of series in next line");
                        int length=Integer.parseInt(in.nextLine());
                        int first=0;
                        int second =1;
                        int element =0;
                        System.out.print(first);
                        System.out.print(" "+second);
                        for (int i = 0; i < length; i++) {
                                    element=first+second;
                                    System.out.print(" "+element);
                                 
                                    first=second;
                                    second=element;
                        }
            }
         


}




In the above program in.nextLine () will return String that’s why we need to convert it in Integer as we need an integer number in for loop.
One more way is there to print Fibonacci series in java is using recursive function which you can find here .


So this was my post on To print Fibonacci series in java.If you have any doubt or you know another efficient method please do write me .If you like this post please comment below  :)

Post a Comment