java - Printing fibonacci series in recursion -
i trying print fibonacci series using recursion , code not ending recursion . can tell me if missed something.i think second recursion going infinite loop , not able figure out why happening
class main { public static void main (string[] args) { int k=7; int x=0,y=1; fib(x,y,k,0); return; } public static void fib(int x,int y,int k,int cnt) { int z; if(cnt>k) return; if(cnt<=k) { z=x+y; x=y; y=z; system.out.println("value is"+z); fib(x,y,k,cnt++); } } }
you don't seem understand concept of fibonacci number. please read wikipedia article. following code function.
public static int fib(int n) { if(n == 0 || n == 1) return n; return fib(n-1) + fib(n-2); }
Comments
Post a Comment