-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |