138. JAVA Program to generate Fibonacci number using static.
How does this program work?
- This program is used to generate Fibonacci number using static in java.
- Fibonacci series means the previous two elements are added to get the next element starting with 0 and 1.
- First we initializing first and second number as 0 and 1, and print them.
- The third number will be the sum of the first two numbers by using loop.
Here is the code
//To generate Fibonacci number using static
public class string{
public static void main(String args[] )
{
int n1=0,n2=1,n3,i,count=10;
System.out.println("Fibonacci Series is:" );
//printing 0 and 1
System.out.print(n1+" "+n2);//loop starts from 2 because 0 and 1 are already printed
for (i=2;i< count; ++i){
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}
}