Print Fibonacci Series.
package com.bethecoder.tutorials.coding.examples; public class FibonacciSeriesTest { /** * @param args */ public static void main(String[] args) { printFibonacciSeries(0, 1, 6); printFibonacciSeries(3, 4, 6); printFibonacciSeries(-2, 4, 6); } private static void printFibonacciSeries(int first, int second, int numOfElements) { System.out.print(first + "\t" + second); int next; for (int i = 0 ; i < numOfElements ; i ++) { next = first + second; first = second; second = next; System.out.print("\t" + next); } System.out.println(); } }
0 1 1 2 3 5 8 13 3 4 7 11 18 29 47 76 -2 4 2 6 8 14 22 36