Fibonacci series program in java ?
Fibonacci series : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 ..........
public class fibonacci {
public static void main(String[] args) {
// 0 1 1 2 3 5 8
int n1 = 0, n2 = 1, range = 20, i = 0, n3;
System.out.print(n1 + " " + n2 + " ");
while (i < range) {
n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.print(n3 + " ");
i++;
}
}
}
Output: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946
Fibonacci Series |
Please comment below to feedback or ask questions.
No comments:
Post a Comment
Please comment below to feedback or ask questions.