In this you are going to learn about the basic java program of Fibonacci series. In this page you can learn Fibonacci series program without using recursion. The steps are given below
- At the first step we have to create a class file and save under some package. eg: Fibonacci.java
- The create a class name and main function.
- Then we have to declare the variables in integer.
- The variable are n1 that was the first number of Fibonacci series n1=0, then n2 that was the second number of the Fibonacci series n2=1, the n3 that we have to find it, then count that was the number of Fibonacci we need, count =10, then i is used in for loop.
- At next step we have to print first two numbers of Fibonacci series.
- Then we have to use the for loop.
- Inside the for lop n3=n1+n2.
- Then print the n3.
- Then use swapping method that is n1=n2, n2=n3.
Program :
package mypack;
public class Fibonacci1 {
public static void main(String args[]) {
int n1=0,n2=1,n3,i,count=10;
System.out.println(n1+"\n"+n2);
for(i=2;i<count;i++)
{
n3=n1+n2;
System.out.println(""+n3);
n1=n2;
n2=n3;
}
}
}
output:
0
1
1
2
3
5
8
13
21
34
إرسال تعليق