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!


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

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

{ 5 comments… read them below or add one }

1 mizan September 18, 2010 at 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)..

2 geet January 23, 2012 at 3:49 am

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

3 Yichao Zhu April 28, 2012 at 1:50 am

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

4 Bijan April 28, 2013 at 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.
Thanks

5 Marcos June 15, 2013 at 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
Regards…

Leave a Comment

Previous post:

Next post: