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 :: create object of hashMap 
Java :: javax.annotation.PreDestroy cannot be found by org.eclipse.e4. 
Java :: discount computation in java 
Java :: on offline event spigot example 
Java :: how to search element in sorted array using java 
Java :: split string in / java 
Java :: javafx check if enter pressed 
Java :: reference to an instance method of an arbitrary object of a particular type 
Java :: JSP Convertir Int a String 
Java :: how do i get DefaultTableModel rows sum in column in java 
Java :: convertir un float en int en java 
Java :: java destroy object 
Java :: edit xmlns attribute with jaxb marshaller 
Java :: connectionpool in jdbc 
Java :: HashMap to Pojo 
Java :: output 
Java :: Java private no-arg constructor 
Java :: Lists - removing with iterator 
Java :: httpsession vs cookie servlet 
Java :: Run the ls command in the terminal to see the uncompiled .java file 
Java :: jdbc code 
Java :: JAVA XML COURSE 
Java :: api for currency conversion free 
Java :: hikari cp oracle jdbc configuration 
Java :: luckperms api get usser by uuid 
Java :: square operator java 
Java :: intellij disable welcome screen 
Java :: struct in java 
Java :: convert java code to kotlin online 
Java :: doubly linked list java add an element to the end 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =