Printing 10 Fibonacii Series numbers without recursion in java
public static void main(String[] args) {
int i=0,j=1,k,count =10;
System.out.print(i+" "+j);
for(int l=2;l<count;l++){
k=i+j;
System.out.print(" "+k);
i=j;
j=k;
}
}
}
output:
0 1 1 2 3 5 8 13 21 34
Printing 10 Fibonacii Series numbers with recursion in java
public class Test{
static int i=0,j=1;
public static void main(String[] args) {
int count =10;
System.out.print(i+" "+j);
printFibonacii(count-2);
}
static void printFibonacii(int count) {
if(count>0){
int k=i+j;
System.out.print(" "+k);
i=j;
j=k;
printFibonacii(count-1);//recursion calling the same method
}
}
}
output:
0 1 1 2 3 5 8 13 21 34
No comments:
Post a Comment