“Fibonacci Java” Code-Antworten

Fibunacci Java

// WARNING: The function assumes that 'fib(0)=0' and 'fib(1)=1'
// Returns the number of the fibonacci sequence at index 'n'
public static int fib(int n) {
	if (n < 2) // No difference if '<' or '<=', because 'fib(2)=2'.
		return n;
	return fib(n-1) + fib(n-2); // Uses the recursion method for solving
}
RedStrix

Fibonacci in Java

// Java program for the above approach
  // divyaraj
class GFG {
  
    // Function to print N Fibonacci Number
    static void Fibonacci(int N)
    {
        int num1 = 0, num2 = 1;
  
        int counter = 0;
  
        // Iterate till counter is N
        while (counter < N) {
  
            // Print the number
            System.out.print(num1 + " ");
  
            // Swap
            int num3 = num2 + num1;
            num1 = num2;
            num2 = num3;
            counter = counter + 1;
        }
    }
  
    // Driver Code
    public static void main(String args[])
    {
        // Given Number N
        int N = 10;
  
        // Function Call
        Fibonacci(N);
    }
}
Ugly Unicorn

Fibonacci -Serie in Java

//Iterative Fibonacci series program in Java

class Fibonacci{  
public static void main(String args[])  
{   
//Considering the first two numbers as 0 and 1 
 int num1=0,num2=1,num3,i,count=10;  
//Count=10 means that only the first 10 fibonacci numbers will be displayed  
 System.out.print(num1+" "+num2);
//printing 0 and 1    
 for(i=2;i<count;++i)  
//looping is initiated from 2 as 0 and 1 are already printed  
 {    
  num3=num1+num2;    
  System.out.print(" "+num3);    
  num1=num2;    
  num2=num3;    
 }    
}}  
Coding Ninjas

Fibonacci Java

import java.util.*;
public class fibonacci{
    public static void main(String args[]){
        int n;
        Scanner sc= new Scanner(System.in);
        n=sc.nextInt();
        sc.close();
        System.out.println(fibo(n));
    }
    public static int fibo(int n){
        if(n==1)return 0;
        else if(n==2)return 1;
        else{
            return fibo(n-1)+fibo(n-2);
        }
    }
}
codelearner

Ähnliche Antworten wie “Fibonacci Java”

Fragen ähnlich wie “Fibonacci Java”

Weitere verwandte Antworten zu “Fibonacci Java” auf Java

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen