Fibonacci Series in Java


Prev Tutorial Next Tutorial

Java Program to print Fibonacci Series

Fibonacci series is in the form of 0, 1, 1, 2, 3, 5, 8, 13, 21,...... To find this series we add two previous terms/digits and get next term/number.

Fibonacci Series in Java

import java.util.Scanner;
class Fibonacci 
{
public static void main(String[] args) 
{
int i,no, first=0, second=1, next;
Scanner s=new Scanner(System.in);
System.out.println("Enter number of terms for Series: ");
no=s.nextInt();
first=0;
second=1;
System.out.println("Fibonacci series are: ");
 for(i=0; i<no; i++)
 {
  System.out.println(first);
  next = first + second;
  first = second;
  second = next;
  }
}
}

Output

Enter nubmer of terms for Series: 
5
Fibonacci series are:
0
1
1
2
3

Syntax to compile and run java program

Syntax

for compile -> c:/>javac Fibonacci.java
for run -> c:/>java Fibonacci

Explanation of Code

  • Scanner s=new Scanner(System.in): are used for receive input from keyboard.
  • nextInt(): method are used for get integer type value from keyboard.
  • System.out.println("....."): are used for display message on screen or console.

Prev Tutorial Next Tutorial

Google Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements