Fibonacci Series Program in Java

 

Program to print fibonacci series.

/**
 * This program is used to print fibonacci series.
 * @author W3spoint
 */
 
public class FibonacciSeries {
      /**
       * This method is used to print fibonacci series.
       * @param num
       */
      static void fibonacci(int num){
            int f1, f2=0, f3=1;
            for(int i=1;i <=num;i++){
                  System.out.println(f3);
                  f1 = f2;
                  f2 = f3;
                  f3 = f1 + f2;
            }
      }     
 
      public static void main(String args[]){
            int num = 6;
            //method call
            if(num > 0){
                  fibonacci(num);
            }else{
                  System.out.println("No. should be greater than zero.");
            }
      }
 
}

Output:

1
1
2
3
5
8

 

Content Protection by DMCA.com
Please Share