-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay11.java
More file actions
54 lines (41 loc) · 1.02 KB
/
Day11.java
File metadata and controls
54 lines (41 loc) · 1.02 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
Day 11 coding Statement: Write a program to find Fibonacci series up to n
Description
Fibonacci series is a special series where nth term is the sum of previous two terms in the series. The series starts with 0 and 1 as the first and second term of the series respectively.
Here you need to get the value for nth term from user and then print Fibonacci series containing n terms.
Input
5
Output
0,1,1,2,3
Input
8
Output
0,1,1,2,3,5,8,13
*/
import java.util.*;
public class Day11
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter a number :");
n=sc.nextInt();
int first=0;
int second=1;
int next;
System.out.println("The fibonacci series of "+n+" is :");
if(n==1)
System.out.println(first);
if(n==2)
System.out.print(first+" "+second+" ");
if(n>=3){
System.out.print(first+" "+second+" ");
for(int i=3;i<=n;i++){
next=first+second;
System.out.print(next+" ");
first=second;
second=next;
}
}
}
}