mylab2

orzhyun · updated January 30, 2023
import java.util.*;
import java.lang.*;
import java.io.*;


class Main {
    private static Scanner scanner;
    
    static void fib(int n){
      int preTwo = 0 , preOne=1 , fibval;
      System.out.print(preTwo+" "+preOne+" ");
      for(int i=2; i<n;i++){
         fibval=preTwo+preOne;
         preTwo = preOne;
         preOne=fibval;
         System.out.print(fibval+ " " );
      }
       
    }
    public static void main(String[] args) {
     scanner = new Scanner(System.in);
     System.out.print("Enter number fib: ");
     int n = scanner.nextInt();
     fib(n);
     }
    }
Output
(Run the program to view its output)

Comments

Please sign up or log in to contribute to the discussion.