The Fibonacci numbers are the numbers in the following integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, ……..
In simple word such series that each element is sum of two previous element is called Fibonacci series.
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
Fn = Fn-1 + Fn-2
package com.javaproficiency;
public class Fibonacciseries {
public static void main(String[] args) {
//Fibonacci series without using Recursive?
int n=10;
int x = 1;
int y = 0;
int z =0;
System.out.print(y +" "+ x);
int i=0;
while(i < n){
z=y+x;
System.out.print(" "+ z);
y=x;
x=z;
i++;
}
}
}
output :
0 1 1 2 3 5 8 13 21 34 55 89
package com.javaproficiency;
public class Fibonacciserieswithrecursion {
public static void main(String[] args) {
int n = 10;
int a =0 ;
int b =1;
System.out.print( a + " " + b);
fibonaci(n, a, b);
}
static void fibonaci(int n , int a ,int b){
int sum = 0;
if (n > 0 ) {
sum = a+b;
a = b;
b = sum;
System.out.print(" "+ sum);
fibonaci(n-1, a, b);
}
}
}
output:
0 1 1 2 3 5 8 13 21 34 55 89
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 141, ……..
In simple word such series that each element is sum of two previous element is called Fibonacci series.
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
Fn = Fn-1 + Fn-2
1. Program to print Fibonacci Series without using Recursion In Java
package com.javaproficiency;
public class Fibonacciseries {
public static void main(String[] args) {
//Fibonacci series without using Recursive?
int n=10;
int x = 1;
int y = 0;
int z =0;
System.out.print(y +" "+ x);
int i=0;
while(i < n){
z=y+x;
System.out.print(" "+ z);
y=x;
x=z;
i++;
}
}
}
output :
0 1 1 2 3 5 8 13 21 34 55 89
2. Program to print Fibonacci Series using Recursion In Java
package com.javaproficiency;
public class Fibonacciserieswithrecursion {
public static void main(String[] args) {
int n = 10;
int a =0 ;
int b =1;
System.out.print( a + " " + b);
fibonaci(n, a, b);
}
static void fibonaci(int n , int a ,int b){
int sum = 0;
if (n > 0 ) {
sum = a+b;
a = b;
b = sum;
System.out.print(" "+ sum);
fibonaci(n-1, a, b);
}
}
}
output:
0 1 1 2 3 5 8 13 21 34 55 89
No comments:
Post a Comment