i want to know how will it give result using recursive in java 1 1 2 3 5 8 13 21 34
fozi
Looking to calculate a Fibonacci sequence?
Last edited by Dilenger4; Jul 3rd, 2003 at 03:37 PM.
Java CodeBank Entries >> Parsing URL's| Collections/ShuffleElements | Threads isAlive() | Daemon Threads |Remote Class Loading | Sorted Keys (Map) | Backwards List | Thread States | Collections/Arrays Generics | Regular Expression(Grouping and Capturing) | Properties | JLabel/JTextField Combo | Reading Request Parameter Values | Host Lookup | Setting the size of a JFrames inner-region | GUI Native L&F
Code: public class Fib{ public static void main(String[] args){ calculate(0,0,0,0); } private static void calculate(int f1, int f2,int temp,int counter){ temp = f2; f2 = f1 + f2; f1 = temp; f1 = f1 + 1; System.out.println(f1); counter = counter + 1; if(counter == 10) return; calculate(f1,f2,temp,counter); } }
public class Fib{ public static void main(String[] args){ calculate(0,0,0,0); } private static void calculate(int f1, int f2,int temp,int counter){ temp = f2; f2 = f1 + f2; f1 = temp; f1 = f1 + 1; System.out.println(f1); counter = counter + 1; if(counter == 10) return; calculate(f1,f2,temp,counter); } }
Forum Rules