forked from twowaits/make-pull-request
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fib.cpp
52 lines (45 loc) · 788 Bytes
/
fib.cpp
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
//Fibonacci Series with a Twist
#include<bits/stdc++.h>
using namespace std;
bool prime(int n)
{
if (n <= 1)
return false;
for (int i = 2; i <= sqrt(n); i++)
if (n % i == 0)
return false;
return true;
}
int fibonacci(int n)
{
int a = 1, b = 1, c, i;
//Corner case
if( n == 0)
return 0;
else if ( n == 1 )
{
cout << "1 ";
return 0;
}
cout << "1 1 ";
for(i = 3; i <= n; i++)
{
c = a + b;
//Condition check
if ( c % 5 != 0 && !prime( c ))
cout << c << " ";
else
cout << "0 ";
a = b;
b = c;
}
return b;
}
int main()
{
int n = 0;
cout << " Please enter the value of n : ";
cin >> n;
fibonacci(n);
return 0;
}