mathajax

Java program Fibonacci series

The successive numbers are generated from its previous two state numbers in the series is called Fibonacci series. Initially, the series has first number =0 and second number =1 and third successive number is generated by summing first and second number in the series.

         
        Initial Fibonacci series F has = {0,1} and successive elements are

             3rd element  = 0+1 =2 
                        F = {0,1,2} 
             4th element  = 1+2 =3 
                        F = {0,1,2,3} 
             5th element  = 2+3 =5 
                        F = {0,1,2,3,5} 
 

Pseudocode - Fibonacci series to positive integer N

  • Read integer N
  • set variable cF=0, pF=1
  • Do
    1. print cF
    2. cF= cF + pF
    3. pF=cF-pF
  • while cF < N

  • Java Program - Fibonacci series

    The Java program generates Fibonacci series of numbers upto given integer N.

     
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    public class Fibonacci {
    
     public static void main(String[] args) throws IOException {
      
     InputStreamReader dis =new InputStreamReader ( System.in );
     BufferedReader br =new BufferedReader(dis);
      
         System.out.println("Find Fibonacci series upto a value n");
         System.out.println("Enter the  value ");    
         String  arg1 =br.readLine();     
         int F = Integer.parseInt(arg1);
         
         int cF=0,pF=1;               
         System.out.println("Fibonocci Series :");
         do {
          System.out.print( cF +" ");
          cF= cF + pF;      
          pF=cF-pF;                  
         } while (cF < F );
     }
    }
    

    Output

    
    Find Fibonacci series upto a value 
    
    Enter the  value 
    15
    Fibonocci Series :
    0 1 1 2 3 5 8 13 
    
    

    Comments

    Popular posts from this blog

    Matrix Forward and Back Substitution

    Chebyshev distance between two points

    Solve System of Linear Equations by LU Decompose

    Complex number Multiplication and Division

    Matrix Determinant, Matrix Adjoint and Matrix Inverse