Fibonacci Series is one of the basic questions in Java., to check the programmer has a basic understanding of logic implementation in java.In Fibonacci series, the current number is always the sum of previous two numbers.Fibonacci series is initialized with 0 & 1.0, 1, (0+1)2, (1+2)3, (2+3)5, (3+5)8, (5+8)13….
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class Fibonocci { public static void main(String args[]) { int previousNumber = 0; int recentNumber = 1; int nextNumber; int count = 20; System.out.print(previousNumber + " " + recentNumber); for (int i = 2; i < count; ++i) nextNumber = previousNumber + recentNumber; System.out.print(" " + nextNumber); previousNumber = recentNumber; recentNumber = nextNumber; } } } |
Output 0 1 1 2 … Continue reading “Fibonacci series“