ADVERTISEMENT
ADVERTISEMENT

Java program to print Fibonacci Series

Print Fibonacci Series in Java - This program will read number of terms and prints the Fibonacci Series. Fibonacci Series is a series in which term is the sum of previous two terms.

Print Fibonacci Series using Java Program

/*Java program to print Fibonacci Series.*/

import java.util.Scanner;

public class Fabonacci {
  public static void main(String[] args) {
    int SeriesNum;
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter the length of fibonacci series : ");
    SeriesNum = sc.nextInt();

    int[] num = new int[SeriesNum];
    num[0] = 0;
    num[1] = 1;
    //number should be sum of last two numbers of Series     
    for (int i = 2; i < SeriesNum; i++) {
      num[i] = num[i - 1] + num[i - 2];
    }

    System.out.println("fibonacci series : ");
    for (int i = 0; i < SeriesNum; i++) {
      System.out.print(num[i] + " ");
    }
    
  }
}

Output:

    Enter the length of fibonacci series : 10
    fibonacci series : 
    0 1 1 2 3 5 8 13 21 34 

Core Java Example Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.