License Public Domain
Lines 32
Permissions
Owner: ragecage
Group Owner: SnortSnort
Viewable by Everyone
Editable by All Siafoo Users
Hide
Siafoo is here to make coding less frustrating and to save you time. Join Siafoo Now or Learn More

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
11print*, 'This program will find the sum of all even Fibonacci numbers'
12print*, 'that have values less than 4 million.'
13
14
15! Initialize the Fibonacci Sequence.
16n(0) = 1
17n(1) = 1
18! Initialize the Sum.
19sum=0
20! Supply the first two lines of output.
21print*, 'Number=', n(0), ' SUM=', sum
22print*, 'Number=', n(1), ' SUM=', sum
23
24do i=2,2000
25! Define the next number in the sequence.
26n(i) = n(i-1) + n(i-2)
27! Exit the loop if the latest number is greater than 4 million.
28if(n(i).ge.4e6) then
29exit
30end if
31! Only add the latest number if it is even.
32if(mod(n(i),2).eq.0) then
33sum=sum+n(i)
34end if
35! Print out the latest fibonacci number and the sum.
36print*, 'Number=', n(i), ' SUM=', sum
37
38end do
39
40print*, 'Fibonacci number now larger than 4 million'
41
42end program fibonacci

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