Menu Close

Fibonacci Series – FORTRAN

To generate the Fibonacci series in Fortran, you can use a loop to calculate each term based on the previous two terms.

 

Fibonacci Series in Fortran

The following program ask the user to enter the number of terms they want in the Fibonacci series.
It then initializes the first two terms (term1 and term2) to 0 and 1, respectively.

The program prints the first two terms and then enters a loop that iterates from 3 to n.

In each iteration, it calculates the next term (next_term) by adding the previous two terms (term1 and term2), and then updates the values of term1 and term2.

The loop continues until the desired number of terms (n) is reached, printing each Fibonacci term along the way.


program fibonacci_series
  implicit none
  integer :: n, i
  integer :: term1, term2, next_term

  ! Read the number of terms from the user
  write(*,*) "Enter the number of terms:"
  read(*,*) n

  ! Initialize the first two terms of the series
  term1 = 0
  term2 = 1

  ! Print the first two terms
  write(*,*) term1
  write(*,*) term2

  ! Generate and print the remaining terms
  do i = 3, n
    next_term = term1 + term2
    write(*,*) next_term
    term1 = term2
    term2 = next_term
  end do

end program fibonacci_series
 

More Related Stuff