First n fibonacci number in JAVA programming


  1. import java.util.Scanner;
  2. public class FibonacciSeries {
  3. public static void main(String[] args) {
  4. Scanner myScanner=new Scanner(System.in);
  5. int n;
  6. System.out.print("Enter number of elements in the Fibonacci series : ");
  7. n=myScanner.nextInt();
  8. int[] arr=new int[n];
  9. arr[0]=0;
  10. arr[1]=1;
  11. int sum=0;
  12. for (int i = 2; i < n; i++) {
  13. arr[i]=sum+(arr[i-1]+arr[i-2]);
  14. }
  15. System.out.print("First "+n+" Fibonacci number are : ");
  16. for (int i = 0; i < n; i++) {
  17. System.out.print(arr[i]+"\t");
  18. }
  19. }
  20. }

  21. //Enter number of elements in the Fibonacci series : 5
  22. //First 5 Fibonacci number are : 0 1 1 2 3
  23.  

No comments:

Post a Comment