Here is a java solution that I got from Scaler for printing the Fibonacci series. But still doubtful.
Please put your valuable insights and help me correct it.
Java:
import java.io.*;
import java.util.*;
public class Main
{
//Recursive function to print the fibonacci series
static int fib(int n)
{
int res;
if(n <= 1)
return n;
else
{
res=fib(n - 1) + fib(n - 2);
}
return res;
}
public static void main(String[] args)
{
System.out.println("\nFIBONACCI SERIES USING RECURSION\n--------------------------------\n");
Scanner sc = new Scanner(System.in);
int n,i = 0;
System.out.println("Enter the number:");
n= sc.nextInt();
System.out.println("\nOutput\n-------”)
while(i++ < n)
{
System.out.println(fib(i) + "");
}
}
}
Please put your valuable insights and help me correct it.