File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments