Calculating fibonacci series in Java
public class Example1 {
public static void main(String [] args) {
fibonacciLoop(5);
}
public static void fibonacciLoop(int n){
if (n == 0) {
System.out.println("0");
} else if (n == 1) {
System.out.println("0, 1, ");
} else {
System.out.print("0, 1, ");
int a = 0;
int b = 1;
for (int i = 1; i < n; i++) {
int nextNumber = a + b;
System.out.print(nextNumber + ", ");
a = b;
b = nextNumber;
}
}
}
Output:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,