-
Notifications
You must be signed in to change notification settings - Fork 109
/
Fibonacci.java
35 lines (30 loc) · 967 Bytes
/
Fibonacci.java
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
26
27
28
29
30
31
32
33
34
35
/* Write a Java program that takes a user input integer n and prints the Fibonacci series up to n.
The Fibonacci series is a series of numbers in which each number is the sum of the two preceding numbers.
The first two numbers in the series are 0 and 1. For example, the first 10 numbers in the Fibonacci series are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34.
The program should use a for loop to generate the series.
Sample Input:
20
Sample Output:
0 1 1 2 3 5 8 13
Explanation:
The Fibonacci series up to 20 is: 0, 1, 1, 2, 3, 5, 8, 13. Therefore, the output is "0 1 1 2 3 5 8 13".
*/
import java.util.*;
public class Fibonacci
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int i = 0;
int k = 1;
System.out.print(i+" ");
while(k<n)
{
System.out.print(k+" ");
int p = k;
k = k+i;
i = p;
}
}
}