Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit ca85ac8

Browse files
committed
Merge pull request #160 from Widea/Widea-fib
Solution to fibonacci problem
2 parents c08967c + 031c984 commit ca85ac8

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

solutions/Fibonacci.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// program to print the fibonacci series
2+
import java.util.Scanner;
3+
4+
public class Fibonacci {
5+
// computes and displays the value at nth position using dynamic programming
6+
static void printSeries(int n) {
7+
long first = 0,second = 1,third = 0;
8+
9+
for (int i = 2 ; i < n ; i++) {
10+
third = first + second;
11+
first = second;
12+
second = third;
13+
}
14+
System.out.print("The number at position " +n+" of the fibonacci series is: " +third);
15+
}
16+
17+
public static void main (String[] a) {
18+
Scanner in = new Scanner(System.in);
19+
System.out.print("Enter the position of the element to displayed in the fibonacci series: ");
20+
int n = in.nextInt();
21+
System.out.println();
22+
printSeries(n);
23+
in.close();
24+
}
25+
}
26+

0 commit comments

Comments
 (0)