Fibonacci series in java
import java.util.Scanner;
public class PracticeDemo {
public static void getFibonacciSeries(int number){
int count=2,first=0,second=1,third=0;
System.out.print(first+" "+second);
if(number>0)
do{
third=first+second;
first=second;
second=third;
count++;
System.out.print(" "+third);
}while(count<number);
}
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
int x=sc.nextInt();
getFibonacciSeries(x);
sc.close();
}
}
Output:Enter the number : 10
0
1
1
2
3
5
8
13
21
34
Comments
Post a Comment