Friday 29 August 2014

Fibonacci Series Program in Java



Fibonacci Series Definition :

Fibonacci series is something like this :
0, 1, 1, 2, 3, 5, 8, 13, 21, 34…….

In this series each term is calculated by adding the previous two terms where the first two terms are 0 and 1.

For example :
Third term = first term + second term = 0 + 1 = 1
Fourth term = second term + third term = 1 + 1 = 2
Fifth term = third term + fourth term = 1 + 2 = 3
And so on……

If we represent this series in Fn than :
F0 = 0
F1 = 1
F2 = F0 + F1
Or in General

Fn = Fn-1 + Fn-2 where n>= 2

Program By using Iteration :

1:  import java.util.Scanner;  
2:    
3:  class FibonacciSeries  
4:  {  
5:    
6:       private void fibonacciSeries(int n)  
7:       {  
8:            int a = 0; //first variable  
9:            int b = 1; //second variable  
10:            int sum;  
11:            System.out.println("The Fibonacci Series is ");  
12:            System.out.print(""+a+" "+b);  
13:            for(int i=1; i<n; i++)  
14:            {  
15:                 sum = a + b;  
16:                 //Here next term which is the sum of previous two is printed  
17:                 System.out.print(" "+sum);  
18:                   
19:                 //first variable takes value of second variable  
20:                 a = b;  
21:                 //second variable takes sum of previous two  
22:                 b = sum;  
23:            }  
24:       }  
25:    
26:       public static void main(String[] args)  
27:       {  
28:              
29:            System.out.println("Enter the no. till you want fibonacci series");  
30:    
31:            Scanner scan = new Scanner(System.in);  
32:            int n = scan.nextInt();  
33:              
34:            //if we have only single term  
35:            if(n==0)  
36:            {  
37:                 System.out.println("The fibonacci Series is \n0");  
38:            }  
39:            //If we want to show two terms  
40:            else if(n==1)  
41:            {  
42:                 System.out.println("The fibonacci Series is \n0 1");  
43:            }  
44:            else  
45:            {  
46:                 FibonacciSeries obj = new FibonacciSeries();  
47:                 obj.fibonacciSeries(n);  
48:            }  
49:              
50:       }  
51:  }  


In the above program we stored the first two terms of the Fibonacci series which are 0 and 1 in two variables (a and b) and print the same if the series you want to calculate is greater than 2.

This program also checks if we want only 1 term or 2 terms in the series and just print the value of variables a or both a and b respectively.


If the series is longer than 2 terms than in the for loop the sum of two variables are calculated and used as a next term which further will be stored in second variable (i.e. b) and second variable value is stored in first variable (i.e. a).

Output :


Program by using Recursion :

Recursion means you called a method from the same method. It basically implemented by the use of stacks.

1:  import java.util.Scanner;  
2:    
3:  public class FibonacciSeriesWithRecursion  
4:  {  
5:       public static void main(String[] args)   
6:       {  
7:            Scanner s = new Scanner(System.in);  
8:           System.out.print("Enter the no. till you want fibonacci series");  
9:                  
10:            //Get the number of terms and stored in variable n  
11:            int n = s.nextInt();  
12:    
13:            FibonacciSeriesWithRecursion obj = new FibonacciSeriesWithRecursion();  
14:                for (int i = 0; i <= n; i++)   
15:            {  
16:                  System.out.print(obj.fibonacci(i) + " ");  
17:                }  
18:         }  
19:    
20:         private int fibonacci(int n)   
21:       {  
22:                if (n == 0)   
23:            {  
24:                  return 0;  
25:                }   
26:            else if (n == 1)   
27:            {  
28:                  return 1;  
29:                }   
30:            else   
31:            {  
32:                 //calling itself with different arguments known as recursion  
33:                  return fibonacci(n - 1) + fibonacci(n - 2);  
34:                }  
35:         }  
36:  }  
37:    


In the above program we uses a method fibonacci which takes the term of series as argument and calculates the sum of previous two terms by calling itself with previous two terms arguments again and again.

Output :


6 comments :

  1. Your new valuable key points imply much a person like me and extremely more to my office workers. With thanks from every one of us.

    Best AWS Training in Chennai | Amazon Web Services Training in Chennai

    ReplyDelete
  2. Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..

    angularjs Training in bangalore

    angularjs Training in btm

    angularjs Training in electronic-city

    angularjs Training in online

    angularjs Training in marathahalli

    ReplyDelete
  3. Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
    python training in velachery | python training institute in chennai



    ReplyDelete
  4. After seeing your article I want to say that the presentation is very good and also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
    Data Science course in rajaji nagar | Data Science with Python course in chenni

    Data Science course in electronic city | Data Science course in USA

    Data science course in pune | Data science course in kalyan nagar

    ReplyDelete
  5. I really appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thx again!

    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete