-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.java
More file actions
49 lines (37 loc) · 885 Bytes
/
Copy pathFibonacci.java
File metadata and controls
49 lines (37 loc) · 885 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package boot;
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
/* Get the nth number in the fibonacci sequence given n
Alternatively given a number F, print out whether it's a fibonacci number and what the closest index n in the
fibonacci sequence is.*/
Scanner num=new Scanner(System.in);
System.out.println("Enter n");
int n=num.nextInt();
System.out.println("EnterF:");
int f=num.nextInt();
if(f<0)
{
System.out.println("Please Enter Positve number");
}
else
{
int x=0, y=1, z=0;
for(int i=2; i<=n; i++)
{
while(z<f)
{
z=x+y;
x=y;
y=z;
}
}
if(z==f)
{
System.out.println(f+"is a Fibonacci");
}
else
System.out.println(f+"not a Fibonacci and closest in sequence is"+z);
}
}
}