Fibonacci Series in Java

In Fibonacci Series the next number is the sum previous two numbers where the first two numbers conprises of 0 and 1. For example : Fibonacci Series of 8  is 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 . etc .

Program for Fibonacci Series

 

import java.util.Scanner;

class Fabonacii {

public static void main(String[] args) {

int num1=0,  num2=1, num3, count;

System.out.print(“Enter the number :”);

Scanner sc =new Scanner(System.in);

count = sc.nextInt();          //Taking input from user

System.out.print(num1+” , “+num2);

for(int i=2;i<count;i++){    //loop starts from 2 because 0 and 1 are already printed

num3 =num1+num2;

System.out.print(” , “+num3);

num1 =num2;

num2 =num3;

}

}

}

One thought on “Fibonacci Series in Java”

Leave a Reply