I was executing this code and also getting the output but along side that I am also getting a exception message i.e.
Output:
0 1 1 2 3 5 8Exception in thread "main" java.lang.StackOverflowError
and after that more than 100s of line telling at com.company.Main.fibnacci(Main.java:24) with exit code 1
package com.company;
import java.util.Scanner;
class Main {
static int a = 0;
static int b = 1;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter no. of terms in fibonacci series ");
int n = sc.nextInt();
System.out.print("0 1 ");
fibnacci(n);
}
public static void fibnacci(int n) throws StackOverflowError {
int c = a + b;
if (n >= 1) {
System.out.print(" " + c);
}
a = b;
b = c;
fibnacci(n - 1);
}
}