-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfib.java
More file actions
46 lines (43 loc) · 1.17 KB
/
Copy pathfib.java
File metadata and controls
46 lines (43 loc) · 1.17 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
//WAP to get the number of fibonacci numbers from the user and print them
import java.util.Scanner;
public class fib
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Please enter the lenghth of the fibonacci series you want: ");
int c = sc.nextInt();
System.out.println("");
int n1 = 0;
int n2 = 1;
int n3 = n1 + n2;
for(int i = 1; i <= c; i++)
{
if(i == 1 && c >= 3)
{
System.out.println(n1);
System.out.println(n2);
System.out.println(n3);
i = 3;
n1 = n2;
n2 = n3;
continue;
}
if(c == 1)
{
System.out.println(n1);
break;
}
if(c == 2)
{
System.out.println(n1);
System.out.println(n2);
break;
}
n3 = n1 + n2;
System.out.println(n3);
n1 = n2;
n2 = n3;
}
}
}