Fibonacci series is achieved by adding the first and second value to solve for the third number. And then the 2nd and third to solve for the 4th number and so on and forth. To be able to achieve this, we need to use to looping statement. In my example, I used the while loop.
The following code will display a Fibonacci series and will stop until the 10th number. Your i there will serve as a counter.
Code
public class Fibonacci {
public static void main(String[] args) {
int a=0;
int b=1;
int c;
int i=1;
System.out.println(b);
while (i<10)
{
c=a+b;
System.out.println(c);
i++;
a=b;
b=c;
}
}}
Output
0 comments:
Post a Comment