Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions JAVA/fibonacci.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.*;
public class Main
{
static int n1=0,n2=1,n3=0;
//Prints Fibonacci Series using Recursion
static void printFibonacci(int n)
{
if(n>0)
{
n3 = n1 + n2;
System.out.print(" "+n3);
n1 = n2;
n2 = n3;
printFibonacci(n-1);
}
}
public static void main(String args[])
{
//Take input from the user
//Create instance of the Scanner class
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n=sc.nextInt(); //Declare and Initialize the number of terms
System.out.print("Fibonacci Series up to "+n+" terms: ");
System.out.print(n1+" "+n2);//printing 0 and 1
printFibonacci(n-2);
}
}