-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge02.c
49 lines (41 loc) · 1.17 KB
/
challenge02.c
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
/* Find sum of even fibonacci numbers less than 4m */
/*And my first C code!!!!*/
/*C code was found to be functional with gcc compiler on ubuntu linux*/
#include<stdio.h>
//Primary function to find fibonacci numbers, determine if they are even, and add as necessary to sum
//Additionally, the code adds fib numbers to an array... the array is not used at this time
void getfib(long a[], int size){
long pushfib = 0;
long tempfiba = 1;
long tempfibc = 2;
// Starting sum at 2 as '2' is left out of our fib loop
long sum = 2;
// capture first two digits of Fibonacci sequence
a[0] = tempfiba;
a[1] = tempfibc;
printf("%ld\n", a[0]);
printf("%ld\n", a[1]);
// create fibonacci numbers
// this loop does all the heavy lifting for this application
for(int i=2;i<size;i++){
pushfib = tempfiba + tempfibc;
if (pushfib >= 4000000){
break;
}
if (pushfib % 2 == 0){
sum = sum + pushfib;
}
a[i] = pushfib;
tempfiba = tempfibc;
tempfibc = pushfib;
printf("%ld\n", a[i]);
}
printf("The sum of all even Fibonacci numbers below 4m is %ld\n", sum);
}
//store variables and main and call getfib function
main()
{
int size = 50;
long fib[size];
getfib(fib,size);
}