Below is a program through which we are printing Fibonacci series in Java.
Code:
Enter the number for Fibonacci series:
8
Fibonacci series
0,1,1,2,3,5,8,13,
Code:
import java.util.Scanner;
public class Fibonacci {
public static void main(String args[])
{
int num;
Scanner scanner=new Scanner(System.in);
System.out.println("Enter the number for Fibonacci series:");
num=scanner.nextInt();
findFibo(num);
}
public static void findFibo(int num)
{
int fibo[]=new int[num];
fibo[0]=0;
fibo[1]=1;
for(int i=2;i<num;i++)
{
fibo[i]=fibo[i-2]+fibo[i-1];
}
System.out.println("Fibonacci series");
for(int i=0;i<num;i++)
{
System.out.print(fibo[i]+",");
}
}
}
Output :Enter the number for Fibonacci series:
8
Fibonacci series
0,1,1,2,3,5,8,13,
No comments:
Post a Comment