-->

9/19/2019

Write a Java Program for Fibonacci series.

Answer: Fibonacci series is a series of numbers where after the initial two numbers, every occurring number is the sum of two preceding numbers.
For Example 0,1,1,2,3,5,8,13,21………
1import java.util.Scanner;
2 
3public class Fibonacci {
4    public static void main(String[] args) {
5        int num, a = 0,b=0, c =1;
6        Scanner in = new Scanner(System.in);
7        System.out.println("Enter the number of times");
8        num = in.nextInt();
9        System.out.println("Fibonacci Series of the number is:");
10        for (int i=0; i<=num; i++) {
11            a = b;
12            b = c;
13            c = a+b;
14            System.out.println(a + "");    //if you want to print on the same line, use print()
15        }      
16    }
17}
Output:
Enter the number of times
9
Fibonacci Series of the number is:
0
1
1
2
3
5
8
13
21
34
NEXT ARTICLE Next Post
PREVIOUS ARTICLE Previous Post
NEXT ARTICLE Next Post
PREVIOUS ARTICLE Previous Post