Fibonacci Series in Java

Advertisements

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

Advertisements

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 8076671483

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

Download Projects