Skip to content

Commit 5dbb98f

Browse files
authored
Create fibonacciSeries.java
1 parent 2dea253 commit 5dbb98f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.Scanner;
2+
public class Main
3+
{
4+
public static void main(String args[])
5+
{
6+
Scanner sc = new Scanner(System.in);
7+
int sum = 0, n;
8+
int a = 0;
9+
int b = 1;
10+
System.out.println("Enter the nth value: ");
11+
n = sc.nextInt();
12+
System.out.println("Fibonacci series: ");
13+
while(sum <= n)
14+
{
15+
System.out.print(sum + " ");
16+
a = b; // swap elements
17+
b = sum;
18+
sum = a + b; // next term is the sum of the last two terms
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)