License Public Domain
Lines 30
Permissions
Owner: ragecage
Group Owner: SnortSnort
Viewable by Everyone
Editable by All Siafoo Users
Hide
Easily highlight source code for your blog with our Syntax Highlighter. Join Siafoo Now or Learn More
Note: You are viewing an old version of this snippet. View Latest Version

Fibonacci problem - Fortran Atom Feed 0

In Brief This program outputs the sum of all even numbers in the Fibonacci sequence that are smaller than 4 Million
# 's
 1! fibonacci.f95
2! Program to find the sum of all even Fibonacci numbers less than 4 million.
3
4
5program fibonacci
6
7implicit none
8! Define the variables.
9integer i,n(0:2000),sum
10
11
12
13! Initialize the Fibonacci Sequence.
14n(0) = 1
15n(1) = 1
16! Initialize the Sum.
17sum=0
18! Supply the first two lines of output.
19print*, 'Number=', n(0), ' SUM=', sum
20print*, 'Number=', n(1), ' SUM=', sum
21
22do i=2,2000
23! Define the next number in the sequence.
24n(i) = n(i-1) + n(i-2)
25! Exit the loop if the latest number is greater than 4 million.
26if(n(i).ge.4e6) then
27exit
28end if
29! Only add the latest number if it is even.
30if(mod(n(i),2).eq.0) then
31sum=sum+n(i)
32end if
33! Print out the latest fibonacci number and the sum.
34print*, 'Number=', n(i), ' SUM=', sum
35
36end do
37
38print*, 'Fibonacci number now larger than 4 million'
39
40end program fibonacci

This program outputs the sum of all even numbers in the Fibonacci sequence that are smaller than 4 Million