Fibonacci Series in Java
Advertisements
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.
Google Advertisment