Fibonacci Series print without using recursion
import java.util.Scanner; public class Fibonacci {
public static void main(String[] args) {
Scanner na = new Scanner(System.in);
int i, a=0,b =1,c,n;
n= na.nextInt();
System.out.println("fibonacci series :- ");
System.out.println(a +"\n"+ b); // print 0 and 1
for (i=3;i<=n;i++){ //loop starts from 3 because 0 and 1 are already printed
c=a+b;
System.out.println(c);
a=b;
b=c;
}
}
}
output:- n=10
Fibonacci series:-
0
1
1
2
3
5
8
13
21
34
0 comments:
Post a Comment