Showing posts with label Program in Java To print Fibonacci series with user input. Show all posts
Showing posts with label Program in Java To print Fibonacci series with user input. Show all posts

Friday, 21 November 2014

Program in Java To print Fibonacci series using recursive method

Program in Java To print Fibonacci series using recursive method
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  using recursive method.

This post is actually an extension of my previous post program in java to printFibonacci series  which you can find here .

But in most of interviews Fibonacci series is asked to print using recursive method .
So What is recursive method ????

Actually as the name signifies a recursive method is one which repeatedly calls itself .While writing a recursive method the most common mistake programmer do is not to properly define the end of that method mean on which condition this method will stop calling itself which can led to Stackoverflow error L

So Please take care of that mistake .So here is program in java to print Fibonacci series  using recursive method


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());
         
          for (int i = 1; i <=length; i++) {
              System.out.print(" "+fibo(i));
             
          }
     }
    
     public static int fibo(int n)
     {
          //System.out.println(n+"n");
          if(n == 1)
          {
          return 0;
          }
          else if(n==2)
          {
              return 1;
              }
          else
          {
              return (fibo(n-1)+fibo(n-2));
          }
     }
    

}





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


you may like :
Java Interview Questions 
Difference between equals() and = =
Read More »

Program in Java To print Fibonacci series

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;
                        }
            }
           
Program in Java To print Fibonacci series

}


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  :)


You may like :
Custom Exception in Java -Tutorial
Read More »