WELCOME everyone who wishes to learn FORTRAN. We will write our first FORTRAN program in the easiest way possible.
You are on this page so I guess you know the meaning of programming language. You choose to see FORTRAN programming language, I guess you know the importance of FORTRAN for the physics. So I will directly take you to write a FORTRAN code.
1. Requirements to write First FORTRAN program code
To write a FORTRAN code, you should have two things with you. The first one is a text editor and the other one is compiler.
a) Text editor
A text editor is where we write our FORTRAN code. You can use any text editor you have in your system or install a new one of your choice. I will be using Ubuntu OS default text editor gedit.
If you don’t have one, you can install gedit from its website.
Or, you can install latest version of gedit using command in terminal. Type
$sudo apt-get install gedit
Compiler is a software that converts our code to the language that computer understands. It is an important tool to write any programming language.
For compiler, I will be using gfortran compiler. you can install other compiler too.
To install gfortran compiler on Ubuntu system, open up terminal using
Ctrl + Shift + T and type
$sudo apt-get install gfortran
After that we are good to start our very first program:
2. Your first FORTRAN program
Like other programming language, the first thing you will learn in FORTRAN program is to display that means, to print out something in the screen.
However, there are two ways to print out on the screen in FORTRAN Programming Language. The first way to do so is by using
print*, statement and other way is using
a) Print*, Statement
print*, statement is widely used to print out on screen. This is easier to write and remember.
b) Write(*,*) Statement
write(*,*) statement, however, is important for other purposes also. It can be used for the output formatting as well as file handling. The two asterisks sign *, * used in write(*,*) have different meanings and hence uses. The former one is used to provide a format to the output whereas the later is used for file handling.
c) Let’s Begin
Ok, let’s start our first code. Open up
gedit and type the below code. Don’t worry, for now, I will explain all those words later.
program first print*, "Hello World!" end program
Then save the with the name of your choice and give extension as
.f95. I will save in the name example.f95. Notice the directory (folder), where you saved the file.
Remember, FORTRAN files should have the extension of
.f95 or you can use .
.f77 for older versions.
If you are using ubuntu system, follow the below steps otherwise you can go directly to the explaining part.
Now open terminal using
Ctrl + Shift + T then go to the directory where you saved your file, and type:
My file name is example.f95. You have to type your file name instead of mine.
Hit enter. If it doesn’t say any error, then congratulation, your first code in FORTRAN is correct. CHEER UP!
Now if you check your folder, you will see a new file with the name
a.out. This is the file created by compiler, a file that computer understands. Now we have to execute it. So type the command once again in the terminal
Then it gives the output. If you see the terminal, it will say:
If you have problems with terminal commands, here is an article for you. Read terminal basic commands.
Now let’s see our previous code and understand in detail.
program first print*, "Hello World!" end program
FORTRAN main code should always lie between program and end program.
program program_name main code......... .............. end program
Give the program name of your choice. But you should not use the reserved words for the program name. Click here to see list of reserved words.
Lets look at the line
print*, "hello world". The
print*, statement says to display something in the screen, we choose to display
hello world text. Remember to surround text using single or double quotes.
You can change your text and and use many
print*, statements, you want
program other print*, "I am Learning FORTRAN" print*, "Printing in FORTRAN is very easy." end program
The output is:
I am learning FORTRAN Printing in FORTRAN is very easy.
The same thing can be obtained using
write(*,*) statement as well.
program other write(*,*) "I am Learning FORTRAN" write(*,*) "Printing in FORTRAN is very easy." end program
write(*,*) statements are written.
If you want to print out the numbers, you don’t need to surround them with a double-quote. for e.g.
program numbers print*, 45 print*, 100.34 end program
But if you consider number as a text, then you must use quote.
program number_and_text print*, "I have 100 apples." print*, "34" end program
I have 100 apples 34
Move on to next tutorial and enjoy coding.
You can view the full playlist of the FORTRAN tutorials on youtube.
If you got any problem above, You can watch this video on YouTube.