using While
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.io.*; class FabSeries{ public static void main(String args[]) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("How may elements you need of Fab Series"); int max = Integer.parseInt(br.readLine()); System.out.println(); // just to print a new line int p1 = 1; int previous = 0; int stop=1; while(stop<=max){ System.out.print((p1+previous) + ","); int temp=p1; p1+=previous; previous=temp; stop++; } } } |
Recursive
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Fibo{ private int Int = 1; private int Add = 0; private void fibo(int x, int Add){ if(x>100) return; System.out.print(x + ","); int temp = x; x +=Add; Add=temp; fibo(x,Add); } public void example(){ fibo(Int,Add); } } |