forked from firstcoder55/code-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.java
More file actions
26 lines (22 loc) · 708 Bytes
/
Copy pathFibonacci.java
File metadata and controls
26 lines (22 loc) · 708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// program to print the fibonacci series
import java.util.Scanner;
public class Fibonacci {
// computes and displays the value at nth position using dynamic programming
static void printSeries(int n) {
long first = 0,second = 1,third = 0;
for (int i = 2 ; i < n ; i++) {
third = first + second;
first = second;
second = third;
}
System.out.print("The number at position " +n+" of the fibonacci series is: " +third);
}
public static void main (String[] a) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the position of the element to displayed in the fibonacci series: ");
int n = in.nextInt();
System.out.println();
printSeries(n);
in.close();
}
}