Java Programming Source Code – Fibonacci Series

Objective:Write a Program using a JAVA code that accepts a number ‘n’ from the user and generates a Fibonacci series for ‘n’ terms:

Fibonacci Series have been around and it is one of the common program that while learning programming every one has to develop. This article talks about program written in pure java, using the concept of classes and methods, along with some useful links.

What is Fibonacci Series? Follow the link to know more about it.

//Program to generate Fibonacci Series for 'n' terms
import java.util.Scanner;
class fibo
{
  public static void fibonacci(int k)
  {
    int x=0;
    int a=0,b=1;
    int i=0;
    System.out.print(a+" "+b+" ");
    for(i=3;i<=k;i++)
    {	
      x=a+b;
      System.out.print(x+" ");
      a=b;
      b=x;
    }
  
  }
  public static void main(String args[])
  {
    Scanner S = new Scanner(System.in);
    int num= S.nextInt();
    fibonacci(num);
  }
}

Note:

  • Just write the above source code, save it by the same name as that of a class. You can also change the name of the class to save it by some other name.
  • Compile the program from command line:
    • javac fibo.java
    • Run the program:
      • java fibo

Some other implementations:

 

Pawan Arora AdministratorKeymaster
Founder , Edukers
Teaching, Coding and Sharing is his passion. A true mentor and motivator. C/C++, Python, Java, Web Technologies (html5 / CSS/ Javascript/ JQuery,Bootstrap, nodeJS ,PHP etc.) and a WordPress enthusiast with more than 18 years of experience.
follow me
October 24, 2018

0 responses on "Java Programming Source Code - Fibonacci Series"

    Leave a Message

    Your email address will not be published. Required fields are marked *