forked from avinashbest/java-coding-ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNthFibonacciSeries.java
More file actions
53 lines (47 loc) · 1.21 KB
/
Copy pathNthFibonacciSeries.java
File metadata and controls
53 lines (47 loc) · 1.21 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
package functionAndScope;
import java.util.Scanner;
/*Fibonacci Number
Given a number N, figure out if it is a member of fibonacci series or not. Return true if the number is member of fibonacci series else false.
Fibonacci Series is defined by the recurrence
F(n) = F(n-1) + F(n-2)
where F(0) = 0 and F(1) = 1
Input Format :
Integer N
Output Format :
true or false
Constraints :
0 <= n <= 10^4
Sample Input 1 :
5
Sample Output 1 :
true
Sample Input 2 :
14
Sample Output 2 :
false */
public class NthFibonacciSeries {
/*How to approach?
We can approach this problem by generating Fibonacci numbers till generated numbers are less
than N.
If the generated number is equal to N, then it is a member of Fibonacci series, otherwise not.*/
public static boolean checkMember(int n) {
int a = 0;
int b = 1;
int c;
while (a < n) {
c = a + b;
a = b;
b = c;
}
if (a == n) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
System.out.println(checkMember(n));
}
}