Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

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);
    }
}
Comment

Fibonacci Series Program. in java

//Fibonacci Series using Recursion
class fibonacci
{
    static int fib(int n)
    {
    if (n <= 1)
       return n;
    return fib(n-1) + fib(n-2);
    }
      
    public static void main (String args[])
    {
    int n = 9;
    System.out.println(fib(n));
    }
}
Comment

fibonacci number 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);
    }
}
Comment

Fibonacci Series 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;    
 }    
}}  
Comment

PREVIOUS NEXT
Code Example
Java :: java singleton 
Java :: largest number in an array in java 
Java :: node in java 
Java :: save bitmap file for share on android 10 
Java :: vscode code formatter cannot format 
Java :: Java try...catch block 
Java :: java create xml 
Java :: how to create an array without knowing the size java 
Java :: destory fragment 
Java :: html alert box android 
Java :: window in java swing 
Java :: how to plus two numbers in java 
Java :: switch statement java 
Java :: java comparing-two-csv-files-in-java 
Java :: write in file java 
Java :: java sort method 
Java :: Float to bytes java 
Java :: write content to file java 
Java :: input using stringbuffer 
Java :: declare function in java 
Java :: Java Exceptions - Try...Catch 
Java :: java anonmyous array eg 
Java :: how to create a java library in intellij 
Java :: neither bindingresult nor plain target object for bean name spring mvc 
Java :: type variable java 
Java :: stack initialization for string java 
Java :: how to read a table from text file in java 
Java :: check if LinkedList is empyth java 
Java :: javafx get actionevent id 
Java :: java regex for ip address 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =