≡ Menu

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

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!"

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

$ 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!

Add your comment

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

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • mizan September 18, 2010, 4:39 pm

    please help me..
    i got a really simple question.

    given natural number n (n ≤ 1000). write these number to word.( seven, eighty six, three hundred fifty five and etc)..

  • geet January 23, 2012, 3:49 am

    while compiling what does the -ffree-form stand for?

  • Yichao Zhu April 28, 2012, 1:50 am

    Thank you very much. A really nice kick-off for me.

  • Bijan April 28, 2013, 2:00 pm

    Hi everybody, does any body know how to run in a release mode instead of debug?
    I need it because of it’s faster speed.

  • Marcos June 15, 2013, 2:44 pm

    Hello Ramesh Natarajan
    Thanks for you lesson, very clear and very easy and above all
    efective, as a start point, it was useful

  • snd February 9, 2017, 9:07 am

    how can show a mathematical program graphically? plz reply me sir