Example:

public class FibonacciSeriesExample {
    
          public static void main(String as[]){
            Scanner s=new Scanner(System.in);
                   int num = s.nextInt(); 
                //taking Values From Keyboard
                  
                             System.out.println("**Fibonacci Series**");
                   int f1, f2=0, f3=1;
                   for(int i=1;i<=num;i++){
                             System.out.print(" "+f3+" ");
                             f1 = f2;
                             f2 = f3;
                             f3 = f1 + f2;
                   }
          }    
    
}

Output:

java FibonacciSeriesExample


5

**Fibonacci Series**
 1  1  2  3  5 

0 comments:

Post a Comment