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 :: private access modifiers 
Java :: spring login response data 
Java :: android activity transition 
Java :: java println format 
Java :: matrix dimensions 
Java :: advantages of using java 
Java :: java 8 lambda comparator 
Java :: Java short Keyword 
Java :: properties object java 
Java :: java heap example 
Java :: for loop in java 
Java :: how to et curent directory in java 
Java :: Map - counting with map 
Java :: androi amterila inpout dialgue to get inouti diaogue 
Java :: exoplayer how to put loader while video is still loading android java 
Java :: intelij show method information 
Java :: actionlistener 
Java :: restore 
Java :: find the key that has the least value in map java 
Java :: var keyword not working in spring boot application 
Java :: android how to get month on world programmatically 
Java :: java GLIBC 2 34 missing 
Java :: random numeros negativos java 
Java :: difference between final and constant in java 
Java :: read only jtextfield 
Java :: java code to create square using 2D array 
Java :: javax.servlet.ServletException: javax.servlet.jsp.JspException: Missing message for key 
Java :: for-each Loop Sytnax JAVA 
Java :: 8233*4 
Java :: Android kotlin send debug log as slack message 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =