Fibonacci series using recursion in java
package
javaprgms;
public class
FibanacciRecursive {
public void fibanci(int n1,int n2){
int sum=0;
if(n1==0){
System.out.println(n1+"\n"+n2);
}
sum=n1+n2;
if(sum<=100){
System.out.print(sum+" ");
n1=n2;
n2=sum;
fibanci(n1,n2);
}
}
public static void main(String[] args) {
FibanacciRecursive fb=new
FibanacciRecursive();
fb.fibanci(0,1);
}
}
Output:
0
1
1
2 3 5
8 13 21
34 55 89
Fibonacci series without using recursion in java
package
javaprgms;
public class
Fibanaciwithoutrecursive {
public static void main(String[] args) {
int n1=0;
int n2=1;
System.out.println(n1);
System.out.println(n2);
for(int i=0;i<=100;i++){
int sum=n1+n2;
if(sum<=100){
n1=n2;
n2=sum;
System.out.print(sum + " ");
}
}
}
}
Output:
0
1
1
2 3 5
8 13 21
34 55 89
No comments:
Post a Comment