write a java program that print the Fibonacci series for a give number

Java program to print Fibonacci series example

In this program, we will learn to print Fibonacci series numbers in java with source code

Source Code: Fibonacci series in java

import java.io.*; 
class Fibonacci {
public static void main(String[] args) throws IOException
{
DataInputStream in=new DataInputStream(System.in);
String s;
int a=1,b=1,c=a+b;
int n;
System.out.print("Enter nth value? ");
s=in.readLine();
n=Integer.parseInt(s);
System.out.print(a+","+b);
while(c<=n)
{
// fibonacci series formula | fibonacci series logic
System.out.print(c+",");
a=b;
b=c;
c=a+b;
}
}
}

Output: Fibonacci series list

Compilation & Execution: D:/cse>javac fibonacci.java
D:/cse>java fibanocci
 
Enter nth value? 21

1,1,2,3,5,8,13,21

Recommended Post:

Find the solution to the salesforce Question and many more

Tags: fibonacci series in java,fibonacci series java,fibonacci series formula,fibonacci series code,fibonacci series logic,fibonacci series numbers,fibonacci series example,fibonacci series list,fibonacci series equation,fibonacci series logic in java

3 thoughts on “write a java program that print the Fibonacci series for a give number”

Leave a Reply

%d bloggers like this: