-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci.java
More file actions
26 lines (26 loc) · 859 Bytes
/
Copy pathfibonacci.java
File metadata and controls
26 lines (26 loc) · 859 Bytes
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
//0, 1, 1, 2, 3, 5, 8 ,...
import java.util.Scanner;
class fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = 0, b = 1, temp = 0, i = 1, l = 0;
System.out.println("Enter the length upto which you want the series to be printed: ");
l = sc.nextInt();
if (l <= 0)
System.out.println("Enter a valid length!!!!");
else {
for (; i <= l; i++) {
if ( i == 1 )
System.out.print("0" + " ");
else if ( i == 2 )
System.out.print('1'+ " ");
else {
System.out.print((a+b) +" ");
temp = a+b;
a = b;
b = temp;
}
}
}
}
}