Skip to content

Commit

Permalink
Fibonacci Series
Browse files Browse the repository at this point in the history
  • Loading branch information
Arijit-SE committed Apr 11, 2023
1 parent ff42a5c commit 75fbb6a
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Fibonacci.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}
}

0 comments on commit 75fbb6a

Please sign in to comment.