Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

fibonacci sequence in java recursion

//Using Recursion
public class FibonacciCalc{
	public static int fibonacciRecursion(int n){
	if(n == 0){
		return 0;
	}
	if(n == 1 || n == 2){
			return 1;
		}
	return fibonacciRecursion(n-2) + fibonacciRecursion(n-1);
	}
    public static void main(String args[]) {
	int maxNumber = 10;
	System.out.print("Fibonacci Series of "+maxNumber+" numbers: ");
	for(int i = 0; i < maxNumber; i++){
			System.out.print(fibonacciRecursion(i) +" ");
		}
	}
}
Comment

fibonacci series in java using recursion

class FibonacciExample2{  
 static int n1=0,n2=1,n3=0;    
 static void printFibonacci(int count){    
    if(count>0){    
         n3 = n1 + n2;    
         n1 = n2;    
         n2 = n3;    
         System.out.print(" "+n3);   
         printFibonacci(count-1);    
     }    
 }    
 public static void main(String args[]){    
  int count=10;    
  System.out.print(n1+" "+n2);//printing 0 and 1    
  printFibonacci(count-2);//n-2 because 2 numbers are already printed   
 }  
}  

0 1 1 2 3 5 8 13 21 34
Comment

recursion java fibonacci

Fn = Fn-1 + Fn-2
Comment

PREVIOUS NEXT
Code Example
Java :: java sort 2d array 
Java :: java shorthand if 
Java :: javafx filechooser specific file 
Java :: list in java 
Java :: how to reverse a list in java 
Java :: How to efficiently find the index of smallest value that is larger than some target within a sorted array, in Java? 
Java :: making android activity fullscreen android studio 
Java :: arraylist string to string array 
Java :: java for loop 
Java :: java days between 
Java :: spring boot jpa in clause 
Java :: minecraft java 
Java :: create date java 
Java :: array to list 
Java :: How to determine if a given binary tree is a binary search tree, in Java? 
Java :: java iterate through map 
Java :: spring jpa count all rows 
Java :: map string list string in java 
Java :: how to loop through a 2d array java 
Java :: java boolean 2d array 
Java :: java log2 
Java :: read file java line 
Java :: java calculate time difference 
Java :: write json file java 
Java :: region in android studio 
Java :: how to make int array java android 
Java :: java guid 
Java :: jframe visibility 
Java :: java remainder sign 
Java :: 2d arraylist in java 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =