In mathematics, the Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers in the following integer sequence:
By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
package com.crunchify.tutorials;
import java.util.Scanner;
/**
* @author Crunchify.com
*/
public class CrunchifyFibonacci {
@SuppressWarnings("resource")
public static void main(String args[]) {
//input to print Fibonacci series upto how many numbers
System.out.print("Enter number upto which Fibonacci series to print: ");
int number = new Scanner(System.in).nextInt();
System.out.println("\n\nFibonacci series upto " + number +" numbers : ");
//printing Fibonacci series upto number
for(int i=1; i<=number; i++){
System.out.print(fibonacciRecusion(i) +" ");
}
}
// Java program for Fibonacci number using recursion.
public static int fibonacciRecusion(int number){
if(number == 1 || number == 2){
return 1;
}
return fibonacciRecusion(number-1) + fibonacciRecusion(number -2); //tail recursion
}
// Java program for Fibonacci number using Loop.
public static int fibonacciLoop(int number){
if(number == 1 || number == 2){
return 1;
}
int fibo1=1, fibo2=1, fibonacci=1;
for(int i= 3; i<= number; i++){
fibonacci = fibo1 + fibo2; //Fibonacci number is sum of previous two Fibonacci number
fibo1 = fibo2;
fibo2 = fibonacci;
}
return fibonacci; //Fibonacci number
}
}
1
2
3
4
|
Enter number upto which Fibonacci series to print: 15
Fibonacci series upto 15 numbers :
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
|
No comments:
Post a Comment