Skip to content

Commit 8a8d06c

Browse files
lakshaygt2013anurag
authored andcommitted
add fibonacci series in java (#109)
closes: #105
1 parent 2450354 commit 8a8d06c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

fibonacci/Fibonacci.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.Scanner;
2+
3+
class Fibonacci {
4+
public static void main(String[] args) {
5+
System.out.print("Enter the number of terms: ");
6+
Scanner scan = new Scanner(System.in);
7+
long num = scan.nextLong();
8+
printFibonacciSeries(num);
9+
}
10+
11+
private static void printFibonacciSeries(long num) {
12+
long a = 1, b = 1;
13+
StringBuilder sb = new StringBuilder();
14+
while (num-- > 0) {
15+
sb.append(a);
16+
if (num > 0) {
17+
sb.append(", ");
18+
}
19+
long tmp = a;
20+
a = b;
21+
b = b + tmp;
22+
}
23+
System.out.println(sb);
24+
}
25+
}

0 commit comments

Comments
 (0)