Don't get spied on – We respect your privacy and provide numerous options to protect it.

# Fibonacci problem - Fortran

Unmodified
Removed
• ## Code

r3 r4
33
44
55program fibonacci
66
77implicit none
8 ! Define the single and double precision variables.
8! Define the variables.
99integer i,n(0:2000),sum
1010
11
12
13! Initialize the Fibonacci Sequence.
1114n(0) = 1
1215n(1) = 1
13
14
16! Initialize the Sum.
1517sum=0
18! Supply the first two lines of output.
1619print*, 'Number=', n(0), ' SUM=', sum
1720print*, 'Number=', n(1), ' SUM=', sum
1821
1922do i=2,2000
20
23! Define the next number in the sequence.
2124n(i) = n(i-1) + n(i-2)
22
25! Exit the loop if the latest number is greater than 4 million.
2326if(n(i).ge.4e6) then
2427exit
2528end if
26
29! Only add the latest number if it is even.
2730if(mod(n(i),2).eq.0) then
2831sum=sum+n(i)
2932end if
30
33! Print out the latest fibonacci number and the sum.
3134print*, 'Number=', n(i), ' SUM=', sum
3235
3336end do
3437
3538print*, 'Fibonacci number now larger than 4 million'