public class Fibonacci_s {
public void generateSeries(int num) {
int f1, f2 = 0, f3 = 1;
for (int i = 1; i <= num; i++) {
System.out.println("fib(" + i + ") = " + f3);
f1 = f2;
f2 = f3;
f3 = f1 + f2;
}
}
public static void main(String[] args) {
System.out.println("*****Fibonnaci Series*****");
Fibonacci_s fb = new Fibonacci_s();
fb.generateSeries(10);
}
}
Output
*****Fibonnaci Series*****
fib(1) = 1
fib(2) = 1
fib(3) = 2
fib(4) = 3
fib(5) = 5
fib(6) = 8
fib(7) = 13
fib(8) = 21
fib(9) = 34
fib(10) = 55
No comments:
Post a Comment