Java program to print Fibonacci sequence Recursive & Non Recursive

The Fibonacci sequence is defined by the following rule. The first 2 values in the sequence are 1, 1. Every subsequent value is the sum of the 2 values preceding it. Write a Java program that uses both recursive and non-recursive functions to print
the nth value of the Fibonacci sequence.

```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 /*Non Recursive Solution*/ import java.util.Scanner; class Fib { public static void main(String args[ ]) { Scanner input=new Scanner(System.in); int i,a=1,b=1,c=0,t; System.out.println("Enter value of t:"); t=input.nextInt(); System.out.print(a); System.out.print(" "+b); for(i=0;i<t-2;i++) { c=a+b; a=b; b=c; System.out.print(" "+c); } System.out.println(); System.out.print(t+"th value of the series is: "+c); } }```
```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 /* Recursive Solution*/ import java.io.*; import java.lang.*; class Demo { int fib(int n) { if(n==1) return (1); else if(n==2) return (1); else return (fib(n-1)+fib(n-2)); } } class RecFibDemo { public static void main(String args[])throws IOException { InputStreamReader obj=new InputStreamReader(System.in); BufferedReader br=new BufferedReader(obj); System.out.println("enter last number"); int n=Integer.parseInt(br.readLine()); Demo ob=new Demo(); System.out.println("fibonacci series is as follows"); int res=0; for(int i=1;i<=n;i++) { res=ob.fib(i); System.out.println(" "+res); } System.out.println(); System.out.println(n+"th value of the series is "+res); } }```

15 Responses to “Java program to print Fibonacci sequence Recursive & Non Recursive”

1. Thank you for your initiative to post this one it’s a great help for my Information Technology studies. thanks a lot.

2. Frank Isack

import java.util.*;
class Rabbit{
int rabbit(int n) {
if(n<=2){
return 1;
}
else{
return rabbit(n-1)+(n-2);
}
}

public static void main(String[]args){
Scanner gr = new Scanner(System.in);
System.out.println("enter last n");
int n = gr.nextInt();
int F1=0,F2=0, res=1;
System.out.println("fibonacci series is as follows");

for(int i=1;i<n;i++) {
F1=F2;
F2=res;
res=F1+F2;
System.out.print(" "+res);
}
System.out.printf("\n");
System.out.println(n+"th value of the series is "+res);
}
}

3. //This program display the Fibonacci series in java language.

import java.util.*;
class Rabbit{
int rabbit(int n) {
if(n<=2){
return 1;
}
else{
return rabbit(n-1)+(n-2);
}
}
public static void main(String[]args){
Scanner gr = new Scanner(System.in);
System.out.println("enter last n");
int n = gr.nextInt();
int F1=0,F2=0, res=1;
System.out.println("fibonacci series is as follows");
for(int i=1;i<n;i++) {
F1=F2;
F2=res;
res=F1+F2;
System.out.print(" "+res);
}
System.out.printf("\n");
System.out.println(n+"th value of the series is "+res);
}
}

4. Sherlyne Pulminar

public class Fibonacci {
public static void main(String[]args){

try {
int prev1=0,prev2=0,prev3=1;

System.out.println(“Enter input:”);

System.out.println(“Fabonacci”);

for (int i=0;i<num;i++){
System.out.println(prev3);

prev1=prev2;
prev2=prev3;
prev3=prev1+prev2;

}
} catch (Exception e){
System.out.print("Error");

}
}
}

5. Edwin Waithanji

Hey guys. Its Edwin, a new learner in java,please send me a Ms word document to explain the whole program that uses both recursive and non recursive functions to print the nth value of a Fibonacci sequence. I will appreciate

6. for(int i=1;i<=n;i++) {
res=ob.fib(i);
System.out.println(" "+res);
}

this is not recursive, we need one function to be called and it shoud be recursive.

7. vijay krishna

HI ranjit,this is vijay krishna.actually in ur fibonocci series recursive program give exception because it takes 0 what it will return.so for my suggestion u can make as n==2 to n==0 in fib function