Fortran Hello World Example: How To Write and Execute Fortran Program on Linux OS

by Ramesh Natarajan on November 13, 2009

Question: I would like to understand the basics of how to write and execute a Fortran program on Linux OS. Can you explain it with a simple example?

Answer: In this article, let us review very quickly how to write a basic Hello World Fortran program and execute *.f program on Linux or Unix OS.

1. Write a Hello World Fortran Program

Create helloworld.f program using the Vim editor as shown below.

$ vim helloworld.f

program hello
print *,"Hello World!"
end program hello

You can also write a shorter version of the hello world Fortran program as shown below.

$ vim helloworld.f

PRINT *,"Hello World!"
END

2. Make sure Fortran compiler is installed on your system

Make sure Fortran compiler is installed on your system as shown below.

$ whereis gfortran
gfortran: /usr/bin/gfortran /usr/share/man/man1/gfortran.1.gz

$ which gfortran
/usr/bin/gfortran

$ dpkg -l | grep gfortran
ii  gfortran                                   4:4.3.3-1ubuntu1                          The GNU Fortran 95 compiler
ii  gfortran-4.3                               4.3.3-5ubuntu4                            The GNU Fortran 95 compiler
ii  libgfortran3                               4.3.3-5ubuntu4                            Runtime library for GNU Fortran applications

Installing GNU Fortran compiler.

If you don’t have GNU Fortran compiler, install it as shown below.

$ sudo apt-get install gfortran

3. Compile the Fortran program

Compile the helloworld.f using gfortran command as shown below. This will create the a.out file.

$ gfortran -ffree-form helloworld.f

$ ls -l
-rw------- 1 ramesh ramesh   55 Oct 24 23:13 helloworld.f
-rwx------ 1 ramesh ramesh 9545 Oct 24 23:14 a.out*

4. Execute the Fortran program (a.out)

$ ./a.out
 Hello World!

$ mv a.out helloworld

$ ./helloworld
 Hello World!
Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

If you enjoyed this article, you might also like..

  1. How To Write, Compile and Execute C Program on Unix OS [With Hello World Example]
  2. How To Write, Compile and Execute C++ Program on Unix OS (With Hello World Example)
  3. Haskell Hello World Example: How To Write, Compile and Execute Haskell Program on Linux OS
  4. Pascal Hello World Example: How To Write, Compile and Execute Pascal Program on Unix OS
  5. Cobol Hello World Example: How To Write, Compile and Execute Cobol Program on Linux OS
  

Vim 101 Hacks Book

Leave a Comment

Previous post:

Next post: