Welcome back to my blog! Today I will show you how to print a Fibonacci series in Java.
Fibonacci series is a series of numbers where an addition of two numbers produces the next number. That next number is added to its previous number and again produces a next number and so on... The first two numbers of Fibonacci series are 0 and 1.
Now let's see how to write the code for this:
public class Series {
public static void main(String args[])
{
int n1 = 0, n2 = 1, n3, i, count = 10;
System.out.println(n1+" "+n2); //printing 0 and 1
for(i=2; i<count; i++) //loop starts from 2 because 0 and 1 are already printed
{
n3= n1+ n2;
System.out.println(" "+n3);
n1 = n2;
n2 = n3;
}
}
}
It will produce the following output:
0 1
1
2
3
5
8
13
21
34
Thank you for coming to my blog. If you find this post useful, please share it with your friends and colleagues.
FEEL LIKE A PROGRAMMER!!!
For more Java tutorials, visit my blog: www.javaoptimist.blogspot.com
Fibonacci series is a series of numbers where an addition of two numbers produces the next number. That next number is added to its previous number and again produces a next number and so on... The first two numbers of Fibonacci series are 0 and 1.
Now let's see how to write the code for this:
public class Series {
public static void main(String args[])
{
int n1 = 0, n2 = 1, n3, i, count = 10;
System.out.println(n1+" "+n2); //printing 0 and 1
for(i=2; i<count; i++) //loop starts from 2 because 0 and 1 are already printed
{
n3= n1+ n2;
System.out.println(" "+n3);
n1 = n2;
n2 = n3;
}
}
}
It will produce the following output:
0 1
1
2
3
5
8
13
21
34
Thank you for coming to my blog. If you find this post useful, please share it with your friends and colleagues.
FEEL LIKE A PROGRAMMER!!!
For more Java tutorials, visit my blog: www.javaoptimist.blogspot.com
Comments
Post a Comment