Answers for "fibonacci program in java"

5

fibonacci series in java

// java program to display fibonacci series using for loop
public class FibonacciSeriesForLoop
{
   public static void main(String[] args)
   {
      int f = 10, num1 = 0, num2 = 1;
      System.out.print("First " + f + " fibonacci sequence: ");
      for(int a = 1; a <= f; ++a)
      {
         System.out.print(num1 + " + ");
         int total = num1 + num2;
         num1 = num2;
         num2 = total;
      }
   }
}
Posted by: Guest on December-05-2020
0

fibonacci sequence algorithm in java

public class Fibonacci_sequence {
	
	static int fib(int n) {
		if( n <= 0 ) {
			return 0;
		}else if( n == 1 ) {
			return 1;
		}else {
			return ( fib( n - 1 )+ fib( n - 2 ));
		}
		
	}
	
	public static void main(String[] args) {
		
		int n = 9;
		System.out.println(fib(n));
		
		// TODO Auto-generated method stub

	}

}
Posted by: Guest on April-05-2021
1

fibonacci series in java

// fibonacci series in java using while loop
public class FibonacciSeriesWhileLoop
{
   public static void main(String[] args)
   {
      int a = 1, num = 15, num1 = 0, num2 = 1;
      System.out.print("First " + num + " fibonacci sequence: ");
      while(a <= num)
      {
         System.out.print(num1 + " , ");
         int total = num1 + num2;
         num1 = num2;
         num2 = total;
         a++;
      }
   }
}
Posted by: Guest on December-05-2020

Code answers related to "fibonacci program in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language