Fibonacci in Fortran

Published on 16 February 2025 (Updated: 15 October 2025)

Welcome to the Fibonacci in Fortran page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

program fibonacci
   implicit none
   integer(kind=8) :: prev, curr, next, n, i
   character(len=32) :: arg
   integer :: ios

   if (command_argument_count() /= 1) then
      call usage()
      return
   endif

   call get_command_argument(1, arg)
   arg = adjustl(trim(arg))
   if (len_trim(arg) == 0) then
      call usage()
      return
   endif

   read(arg, *, iostat=ios) n
   if (ios /= 0 .or. n < 0) then
      call usage()
      return
   endif

   if (n == 0) return

   prev = 0
   curr = 1

   do i = 1, n
      write(*,'(I0,": ",I0)') i, curr
      next = prev + curr
      prev = curr
      curr = next
   end do

contains

   subroutine usage()
      write(*,'(A)') "Usage: please input the count of fibonacci numbers to output"
   end subroutine usage

end program fibonacci

Fibonacci in Fortran was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.