How to Write a Fortran Program

How to Write a Fortran Program thumbnail
Fortran programming is relatively easy to learn.

Published in 1957, Fortran, an acronym for "formula translation," is a procedural and imperative programming language. Targeted towards scientific computing and numeric computation, Fortran is a very popular language used in high-performance computing. Furthermore, it is usually the language in which programs that benchmark the world's fastest computers are written. Featuring efficient execution, Fortran is easy to learn and machine independent.

Instructions

    • 1

      Open Notepad or your favorite text editor and write the following lines:

      program hello

      print *, 'Hello!'

      stop

      end program hello

      Save this in a file that has the extension ".f" or ".for". This is the easiest program you can ever write, and it just prints "Hello!" on the screen. As you can see, the structure of the program is:

      program name

      declarations

      statements

      stop

      end

    • 2

      Add a new line to your new program:

      print *, 'This is my 1st program..'

      You can also separate statements by using semicolons, like this:

      print *, 'This is my 1st program..' ; print *, 'Hello!'

      Your new version of the program is now:

      program hello

      print *, 'This is my 1st program..' ; print *, 'Hello!'

      stop

      end program hello

      Although the "stop" statement is not needed because the program reaches the end in any case, it is best practice to terminate the program with this statement. It clearly shows where the execution flow stops.

    • 3

      Replace the "print" line in your program with this one:

      print *, 23.6*log(4.2)/(3.0+2.1)

      Computers can do mathematics, and Fortran programs are, of course, able to use this. This is a simple mathematical function, and your program will display the result on your screen. Of course, precedence of arithmetic operations applies. "*" is used for multiplying, "/" is used for division and "**" is used to raise a base to a power. "+" and "-" are used for addition and subtraction, respectively.

    • 4

      Add some text before the result to explain what that result is. The following line is a good example of how it should look:

      print *, 'The result of 23.6*log(4.2)/(3.0+2.1) is ', 23.6*log(4.2)/(3.0+2.1)

      Your program should now look like this:

      program hello

      print *, 'The result of 23.6*log(4.2)/(3.0+2.1) is ', 23.6*log(4.2)/(3.0+2.1)

      stop

      end program hello

    • 5

      Add new lines to your program and play with mathematical functions until you fully understand how to use them. You now know the basics of writing a Fortran program, but there are still many things to learn. There are dozens of resources available, and it should be just a matter of days before you begin writing more complex programs.

Related Searches:

References

  • Photo Credit Comstock/Comstock/Getty Images

Comments

Related Ads

Featured