You are on page 1of 38

Chapter 11.

Programming Language: FORTRAN


Introduction:
Fortran is a general purpose programming language, mainly
intended for mathematical computations in e.g. engineering.
Fortran is an acronym for FORmula TRANslation, and was originally
capitalized as FORTRAN. However, following the current trend to
only capitalize the first letter in acronyms, we will call it Fortran .
FORTRAN Generation
The oldest FORTRAN was extended to FORTRAN II .
Again, FORTRAN II was extended to FORTRAN IV by
incorporating double precision and some more features. At
that time, COBOL and BASIC computer programming
languages were being used for business purposes.
Again, FORTRAN was extended FORTRAN 66 and then to
FORTRAN 77 by adding some useful features like file
handling..etc. The last two digits , 77 represents the year
of the version development. This version of FORTRAN
satisfies all the programmers thats why it became popular
version of FORTRAN.
FORTRAN 77 was further extended to FORTRAN 90 by some
companies but it was suppressed in the use by the
FORTRAN 77.
As, FORTRAN 90 was not declared officially, it was again
modified to FORTRAN 95 which was expected to be
approved by ANSI soon(1996).
Also, several versions of Fortran aimed at parallel
computers. The most important one is High performance
Fortran (HPF).
11.1 Character Set:
The set of characters using in FORTRAN are following:
Capital alphabets: A to Z
Digits : 0 to 9
Symbols: + - / * . , _ $ ( )
11.2 Data types, Constants and variables

Prepared by Er. Om Prakash Mahato Page 1


Data types may be either REAL(float in C), INTEGER ,
CHARACTER , COMPLEX , LOGICAL etc.
Constants: a number on a string of FORTRAN characters is called a
constant. Numbers are called numeric constants. A string of
characters is called a character constant.
FORTRAN CONSTRANT can be classified as follows:

a. INTEGER CONSTANT: integers written without decimal point


are called fixed point constants or integer constants. In other
words, integer constants are whole numbers without any
decimal point. -23,4,0 and 98 are some examples of integer
constants. The maximum length of integer depends upon the
world length of the computer.
b. REAL constant: any number written with one decimal point is
called a floating point constant or real constant. For example
-12.78, 8.5678, 3.4E4 and 140.0 are some real constants. A
real constant can be expressed in any one of the following
two forms
i)fractional form: e.g. 12.3, 10.0 e.tc.
ii) exponential form: e.g. 10.2E02 which is exactly 10.2102.
c. Character constants: character constants are any string of
character enclosed within quotes. It is noted that only single
quote() must be used and not the double quote(). The
maximum length of the character is 127.

Prepared by Er. Om Prakash Mahato Page 2


Fortran Variable:
The name of the memory location is called variable. For example
A, B, C and D are called variables.
Rules for naming variables:
1. Variables names can be from one to six characters in length
2. The first character of the variable name must be an alphabet
and the succeeding characters can be alphabets or numeric
digits.
3. No special character is allowed in a variable name.
4. Fortran verbs(keyword reserved by Fortran for special
meaning) cannot be used as variable names.
5. Fortran is case independent programming language i.e. the
variable SUM and sum have same target i.e. same memory
location.
Types of variables
There are three types of variables
i) Integer variable
ii) Real variable
iii) Character variable

Variable type declaration:


Integers generally occupy less memory storage than real
numbers. So to make a program memory efficient, the variable
should be declared at beginning of the program i.e. using integers
instead real if possible for faster or proper memory management
purposes.
In Fortran, it is not compulsory to declare variables. if the
variables are not declared, the variables names starting with
letters I,J,K ,K,L,M or N are considered to be integer variables and
others as real variables. for character variables , the declaration is
compulsory.
The general format for declaring integer variables is shown below:

INTEGER list of variables

Prepared by Er. Om Prakash Mahato Page 3


If you want to make I, SUM, AREA to be integer variables, this can
be declared as
INTEGER I, SUM, AREA.
You can declare SIZE and CIRCUM as real variable as follows:
REAL SIZE ,CIRCUM
The general format is shown below:

REAL list of variables


Declaration statement of the character variable also contains the
length of the string that the variable can hold. If the length is not
specified, the length is considered as 1. For example, if NAME is a
character variable which will hold the names of length at most 25
characters then must be de clared as follows:
CHARACTER NAME*25
If one or more character variables have the same length the
asterisk(*) can be placed near the word CHARACTER.
Implicit type declaration
In the type of declaration seen so far we explicitly list all the
variables and their type. There is another way of implicitly
declaring the type of several variables in one simple declaration.
The general format is given below
IMPLICIT type(-.), type(-.),
The following examples make the concept clear
i) IMPLICIT INTEGER(A)
The above statement declares that all the variables names
starting with the alphabet A are integer variables. This
does not affect the general rule that the variables starting
with the letters I,J,k,L,M or N are also integer variables.
so, the above statement declares that all the variables
starting with the letters A,I,J,K,L,M or N are integer
variable in the program.
ii) IMPCLICIT INTEGER(C,E-G)

Prepared by Er. Om Prakash Mahato Page 4


This declares that the variable names starting with the
letters C,E,F,G or I,J,K,L,M or integer variables.

11.3 Arithmetic operations, Library Functions


11.3.1. Arithmetic Expressions:
All the mathematical operations are allowed in FORTRAN. The
following is the list of symbols used for the arithmetic operations.
+ (plus) Addition e.g. A+B
-(minus) Subtraction e.g. A-B
*(asterisk) Multiplication e.g. A*B
/(slash) Division e.g. A/B
**(double asterisk) Exponentiation e.g. A**B (=A B)
Hierarchy of Arithmetic operations:
The following is the rule of Hierarchy arithmetic operators in
FORTRAN
1. Any expression within parenthesis is first evaluated.
2. Exponentiation is given the top priority and evaluated first in
an expression.
3. Multiplication and division are given the next priority.
4. Addition and subtraction are performed finally.

Mode of arithmetic expression


1. Integer mode: containing only integers e.g. 1+2
2. Real mode: containing only real numbers e.g. 2.0+3.0 or 2.+3.
3. Mixed mode: containing integer and real
For example , 3/2. , in the operation of this
mode, at first 3 is converted into real implicitly called
promotion and then 3.0/2.0 is performed in real mode.

11.3.2. Library Functions:


FORTRAN allows some mathematical inbuilt functions. These
functions are called Library functions. The compiler itself evaluates
these functions. For example, SQRT(X) is a Library function which
gives the square root value of X.

Prepared by Er. Om Prakash Mahato Page 5


Some library functions are tabulated below:
Function Meaning Argument Value
SQRT(X) X Real Real
EXP(X) ex Real Real
log x
LOG(X) e Real x>0 Real
log
LOG10(X) 10x Real x>0 Real
ABS(X) |x| - -
sin(x)
SIN(X) Real x in radian Real
cos(x)
COS(X) Real x in radian Real
tan(x)
TAN(X) Real x in radian Real
sin-1(x)
ASIN(X) Real (-1x1) Real in radian
cos-1(x)
ACOS(X) Real (-1x1) Real in radian
Sinh(X)
SINH(X) real Real
remainder from division
MOD(x,y) integer Integer
x by y.

11.3.3 Arithmetic Statements (Assignment Statements):


The FORTRAN arithmetic statement is used to evaluate a valid
FORTRAN expression and store the result at a desired variable. The
general form of an arithmetic statement is
Variable=expression
Where the symbol = is used to assign the value so , called assignment
operator.
11.4 Structure of a Fortran Program
Up to now, we have discussed about the basic sub-topics, like data
type, constants, variables, Arithmetic expressions and library
functions. Now, it is time to discuss about the particular instruction
which is used to perform a particular task. A Fo rtran program consists
number of instructions which are called statements of the program.
The statements are written one below the other in Fortran program.
It is noted that the ending of the statement doesnt contain semicolon
(;) like in C programming language.

Prepared by Er. Om Prakash Mahato Page 6


11.4.1 FORTRAN coding sheet
FORTRAN coding sheet is a sheet of a paper with 80 columns in
each line of FORTRAN program. i.e. we can write only 80 characters in
one line.
1 2 3 4 5 6 7 8 9 10 11 12 -
C s a M p l e P r o g r a m
p r I n t h E l l o
s t O P
e n D
Table 11.1: FORTRAN coding sheet
The Table 11.1 shows the format of Fortran Coding sheet or structure
of Fortran program. As shown in above coding sheet, if the character
c is written on the first column any line (row), then that line is
treated as the comment line and it is ignored by the compiler.
Among 80 columns of FORTRAN coding sheet, from 7th column to 72 th
columns are only used for FORTRAN statements. Columns 73 to 80 are
ignored by the compiler so, it these columns can be used for the
comment of the programmer. Columns 1 to 5 are used for giving line
numbers in FORTRAN, it is not necessary to put the line number in
each line of the program. Only the statements which are latter
referred (label) can be given line numbers. The line number is a
positive integer with at most 5 digits. Also, it noted that the line
numbers need not be given in ascending order. They can be in any
order.
The 6th column is used for line continuation exclusively in case of the
long statement (having more characters than 7 to 72 columns). For
this purpose, any one character must be put in the 6th column so that
the corresponding line is treated as the continuation of the previous
statement.
For better understanding of the structure of FORTRAN program, it is
recommended to download the FORTRAN compiler FORCE 2.0 or any
other one to visualize the FORTRAN coding sheet. A example of simple
program in the FORCE 2.0 is shown in following figure 11.1.

Prepared by Er. Om Prakash Mahato Page 7


Figure 11.1 a simple program in FORTRAN coding sheet.
In above program, c makes the first line as comment line i.e. heading
of the program. Second line is the output statement line started from
7th column is exactly like printf in C programming which prints Hello
World in black screen after compiling and running the program. The
third line has been started from the 8th column i.e. STOP statement
which terminates the program execution. The last line is the END
statement which informs the computer as last line of program.
The difference between STOP and END is as tabulated below:
STOP END
1. Used to instruct the 1. Used to inform the
computer to stop the computer that this is the
execution of the program last line of the program.
2. STOP statement may 2. END statement occurs
occur anywhere in the only at the end of the
program. statement.
3. Any number of STOP 3. Only one END statement
statements may occur in must occur in the
the program. program.

Note: in above figure 11.1 , at the bottom of the coding sheet, the row
number and column number of cursor position is displayed as 3:8 (3 rd
row and 8th column) for STOP statement.

11.5 Formatted and Unformatted Input/Output Statements


11.5.1 Format specification:

Prepared by Er. Om Prakash Mahato Page 8


While entering or displaying the data, it is to be fully mentioned the
type of the data (integer, real and character) and its size . Such type of
specification i.e. type of the data and its size is called format
specification. This is non-executable statement.
For example: -2.345 is real constant having negative sign, total
width=6(all characters i.e. sign, numeric digit and decimal point) and
decimal width=3. Similarly, -567 is an integer constant having width of
4(one for sign character and 3 for digits characters).
11.5.2 FORMAT statement
We have so far discussed about specification. Now, it is a question
that how data is formatted in FORTRAN programming. For this
purpose, there is a FORMAT statement in FORTRAN. A general form of
a FORTAT statement is
n FORMAT statement(f1,f2,f3fr)
when n is the statement number and f1,f2,f3.fr are the format
specifications.
Rules for FORMAT specifications
1. The format specifications f1,f2,fr must be enclosed
within parenthesis.
2. The specifications f1,f2,fr must be separated by commas.
3. Every FORMAT statement must be given a statement number.
Format types in Fortran
Basically, following formats are used in FORTRAN
1. I format
2. F format
3. E format
4. X format
5. / (slash) format
6. A format
7. T format
8. Quote format.
Now, lets discuss all above formats individually.
1. I format:

Prepared by Er. Om Prakash Mahato Page 9


The symbol I represents integer i.e. I format is used for
integer quantity. The general format specification is
Iw
Where w is the width of the integer data including one space
for sign.
For examples:
i) Suppose, We have three integer quantities typed in a
single line as follows:
- 1 2 1 4 4 5
Here, three integers -12, 14, 45 are shown in single
line. To display in this format, we can write format
statement as
FORMAT (I3, I3, I3)
Where I represent integers and 3 represents width of
integer. Also, it is noted that the integer is printed
from the right only after allocating the 3 spaces in
above case, thats why one space is remained blank
between two integers.
Also, one another point is noted that in above
statement, there is a continuous three integers of
equal widths. So, it can be also written as:
FORMAT(3I3).
ii) Suppose , We have another four integer quantities
typed in a line as follows:
3 5 7 + 2 5 - 7 2 - 1 5 8 1
For displaying as above four integers 357,+25,-72,-1581 in a
line, the FORMAT statement can be written as
FORMAT(I4,I3,I3,I5)
Here, I4 allocates 4 spaces at first and then prints 357 in 3 places from
right and one space is remained blank.
The above format can also be written as
FORMAT(I4,2I3,I5)
This is possible because there is two continuous integers of equal
width i.e. I3 and I3 thats why it can be combined as 2I3. This gives the
same effect in the output.
Prepared by Er. Om Prakash Mahato Page 10
2. F format:
The symbol F represents real data in Fractional form. The
general form of F format statement is
F w.d
Where
w is the total width of the number(including sign, decimal
point and digits).
d is the de cimal width
For examples:
i) Consider the number -72.567
This is the real data in fractional form having
Total width=7(including digits, sign and decimal point)
Decimal width=3
So, this data can be represented by the format F7.3
ii) Consider the following data
2 5 . 6 - 8 . 9 9 - 8 3 3 . 7 2
Here, three numbers 25.6, -8.99,-833.72 which take columns
1 to 5, 6 to 10 and 11 to 17 respectively.
As shown in first example, we can write format for these
three numbers as
25.6 F5.1
-8.99 F5.2
-833.2 F7.2
So, the above display of three real numbers in one line, it
can written in the following F format statement
FORMAT(F5.1,F5.2,F7.2)
Note: as discussed in I FORMAT, if there is multiple
continuous same formats of multiple data then we can also
write in combined form instead of repeated form.
For example: consider the following data
- 8 . 1 + 1 . 5 - 8 . 9
For above display, the FORMAT statement can be written as
FORMAT (F4.1, F4.1, F4.1)
Here, same format is continuous so, the above format
statement can also be written in combined form as
Prepared by Er. Om Prakash Mahato Page 11
FORMAT(3F4.1)
Here, 3 represents the 3 times repetition of same format F4.1
3. E format:
The general form of E format is as follows:
E w.d
Where,
-The alphabet E represents real data expressed in exponential
form.
-w is the total width including mantissa and letter E, exponent
and exponent sign.
-d is the decimal width of the mantissa.
For examples:
i) Consider one real number expressed in exponential form as

Here, total width is 13


Decimal width of mantissa is 5
So, this number can be represented by the format E13.5
Note: exponent is always an integer with at most two digits.
ii) Consider the following data
- 2 . 5 E - 8 9 4 . 3 2 E 5
Here, two real numbers in exponential form are -2.5E-
89 and 4.32E5 which take the columns from 1 to 8
and 9 to 14 respectively.
As shown in first example, we can write format for these two
numbers as
-2.5E-89 E8.1
4.32E5 E6.2
So, for above display of two real numbers in one line, it can written
in the following E format statement as
FORMAT(E8.1,E6.2)
iii) Consider the data
5 . 5 2 E - 1 2 - 1 . 5 7 E - 1 3

Prepared by Er. Om Prakash Mahato Page 12


Here, the two numbers, 5.52E-12 and -1.57E-13 are
shown in one line, which take the columns from 1 to 9
and 10 to 18 respectively.
The E format for these two numbers are E9.2 and E9.2
which are same thats why these are represented by the
statement
FORMAT(E9.2, E9.2)
As discussed earlier, This can also written as
FORMAT(2E9.2)
4. X format:
The general form of the X format is given as
nX
where X is a symbol which is used to skip some columns in a
data record i.e. to make blank space.
And n is the number of columns to be skipped.
This is used in formatted output statement which causes n
blank spaces in the output.
For example:
i) Suppose two numbers -72 and +814 are in a record as
follows
- 7 5 + 8 1 4
Here, the integer number -72 takes the columns 1 to
3 in format I3. And the number +814 take the
columns 7 to 10 in format I4. There is skipped of some
columns i.e. 4 to 6.
To achieve the above display, the format statement
must be
FORMAT(I3,3X,I4)
Here, 3X makes 3 black spaces between two numbers.
ii) Consider one format
FORMAT(3X,I3,F5.2,2X,E5.1)
Suppose the numbers +12,-2.34 and 1.1E1 are to be
printed according to the above format. Then the
printed output will be as follows:

Prepared by Er. Om Prakash Mahato Page 13


+ 1 2 - 2 . 3 4 1 . 1 E 1

5. /(slash) format:
This is format is used to change the line which is exactly as \n
used for same purpose in C programming.
It can be used either just after the number format or placing
the comma as
1. FORMAT(I2/,I4) which prints I2 in one line and line
changes so, I4 is printed in next line.
2. FORMAT(I2,/,I4) which has same effect as above i.e. prints
I2 in one line and line changes and I4 is printed in next
line.
If you put double slash i.e. // then its effect will exactly
like \n\n in C programming which changes two lines. In
the same way, we can put triple slash(///) to change 3
lines and so on.

Remember! /w where w=number of slash (e.g. /2 ) and w/ (e.g. 2/)


cannot be used instead of //. In case of 2/ , the program is compiled
without any error but run time error is occurred. But in case of /2 ,
compiler detects the error.

Lets discuss some example of / format.


i) Suppose we have two numbers 123 and -2345. If we want
to print 123 in first line and -2345 in next line then the
format must be either
FORMAT (I3/,I5) or FORMAT(I3,/,I5)
ii) Consider the statement FORMAT(3X,I3,///,1X,F5.2).
Suppose, we have two number +21 and -3.14 then output
will be
First line + 2 1
Second line
Third line
Fourth line - 3 . 1 4

Prepared by Er. Om Prakash Mahato Page 14


In above, output, in first line 3 spaces are created due 3X
and then +21 is printed by I3. /// changes line 3 times and
therefore 2nd and 3rd lines remained blank and the cursor
is moved at start of fourth line. In fourth line, one space is
created due to 1X and then -3.14 is printed due to F5.2.
6. A format
The general form of the A format is given by
Aw
Where A is used for character data and w is the width. In this
format w is optional. If w is not specified then the size of the
variable the variable declared in CHARACTER statement is
considered as the width.
Suppose a variable C is de clared as
CHARACTER C*7
C=Fortran
In an output statement if C is to be printed according the
format
FORMAT(A)
Then the output will be obtained as
F O R t r a n
Here the width will be considered by the CHARACTER statement.
For the same value, if the format is
FORMAT(A4)
The printout will be
F O R t
7. T format
The general format of T format is given by
Tn
Where n is the column from which the output must start. This
is used only for the output statement.
For example, consider the format statement
FORMAT(F5.2,T8,I2)
The first value will be printed according to F5.2 format. The
second value will start from the 8th column due to T8. It is
noted that 8 blank are not left. The second value will start to
Prepared by Er. Om Prakash Mahato Page 15
print from 8th column. Suppose, first value is -3.14 and second
value is 21 then the output will be as follows:

For the same two values if we change T8 to T10 in above


format statement i.e.
FORMAT(F5.2,T10,I2)
Then, the output will be as follows:

Here, due to T10 , the value following T10 only starts from
10th column and more two blanks are left than previous
output.

8. Quote format
To print any message, quote format is used. Anything kept
within single quotes is printed as it is.
For example, if we want print, Hello world then we can write
Quote format as
FORMAT (Hello world)
This is very useful format for giving the clear meaning while
displaying any result.
For example, if we have AREA value to printed then we can
write Format statement for this as
FORMAT (The area of given rectangle is, F10.2,sq.cm.)
In the output, it will be like
The area of given rectangle is 1234567.24

Prepared by Er. Om Prakash Mahato Page 16


11.5.3 Input/ output statement:
Input/output statements in Fortran can classified as
follows:

11.5.3.1 Formatted I/O:


1. Formatted READ Statement: it is input statement i.e. it is
used for transferring the data from the input device to the main
memory of the computer.
The general form of the read statement is ,
READ(h, n) var1,var2
Where
h is an integer called hardware number
n is the line number of the FORMAT statement
var1, var2.are the variables.

Hardware number: A computer may be having more than one input devices e.g.
card reader, magnetic tape, key board etc. For selecting particular hardware
device, a unique hardware number is used like 1 for card number, 2 for
magnetic tape and 3 for the keyboard and monitor (VDU). In personal
computers, usually the keyboard is the only input device. So we put the
asterisk symbol (*) in the place of hardware number.

Examples:
i) Read a integer number of 3 digits from keyboard.
ii) Read two real numbers of total width 10 and decimal
width 3.

Prepared by Er. Om Prakash Mahato Page 17


To write programming code for above reading statements, we have to
use both READ as well as FORMAT statements as shown below:
i) READ(*,2)M
2 FORMAT(I3)
Here, variable M must be in the format I3 from the
keyboard i.e. value must be of 3 digits. 2 written within
READ statement is as a label but is written as line number
which references format statement for the variable of
READ statement. it is noted that line number may any
one of maximum five digits. Also, one format statement
can be referenced by any number of READ statements
and it may be anywhere in the program before end
statement.
ii) READ(*,6) A,B
6 FORMAT(F10.3,F10.3) or FORMAT(2F10.3)

When this statement is encountered, the computer waits


for data. The cursor will be blinking. We must now type A
and B in the format F10.3 format in the same line.
Suppose we type
-12345.123 -54321.321
And after pressing return key (enter key), -12345.123 is
assigned in A and -54321.321 is assigned in B.
While using the latest compiler like FORCE 2.0, we also
type as -12345.123, -54321.321 where comma (,)
separates two numbers.
Rules of Formatted READ statement
READ statement should have its corresponding
FORMAT statement.
The variables listed in the READ statement must
have a one to one correspondence with matching
format specifications given in the FORMAT
statement.

Prepared by Er. Om Prakash Mahato Page 18


2. Formatted WRITE Statement: it is output statement i.e. it is used
for transferring the data from the main memory of the computer to
the output device.
The general form of the read statement is ,
WRITE(h, n) var1,var2
Where
h is an integer called hardware number (explained earlier)
n is the line number of the FORMAT statement
var1, var2.are the variables.
Examples:
i)Consider the Write statement given below:
WRITE(*,5) A,B
5 FORMAT(F5.2,2X,F4.1)
As explained in READ statement section, * is used for standard input
output devices i.e. keyboard for input and monitor (VDU: Video
display unit) for output device. 5 is the line number which
references the format of variables A, B of WRITE statement.
Suppose A=12.12 and B=12.3 then the output of the above write
statement will be as
1 2 . 1 2 1 2 . 3
2X in above FORMAT statement makes two blank spaces between
two numbers.
As discussed in formatted input statement (READ statement), In
WRITE statement, Also, there can be at most one format
statement anywhere (but referenced by line number) in the
program for more than one WRITE statement.
ii) Consider the WRITE statement
WRITE(*,4) Y
4 FORMAT(The square root of 6.25 is ,F3.1)
Here, the output will be
The square root of 6.25 is 2.5
iii)Consider the WRITE statement
WRITE(*,8)
8 FORMAT(Welcome to students in TU)
This statement displays the following message on the screen
Prepared by Er. Om Prakash Mahato Page 19
Welcome to students in TU
iv)Consider the WRITE statement
WRITE(*,5)
5 FORMAT(enter the value of x and y in the format F6.2 and F 5.3
respectively)
This statement prints the following message on the screen
enter the value of x and y in the format F6.2 and F 5.3 respectively
This type message is useful for entering the data correctly.
v) Consider the WRITE statement
WRITE(*,25) S,D
25 FORMAT(The sum of two numbers is: ,F4.2/,The difference
of two numbers is: ,F4.2)
Here, Suppose S=3.14 and D=1.28 then, the output of above
statement is
The sum of two numbers is: 3.14
The difference of two numbers is 1.28
vi) consider write statement
WRITE(*,*)enter the value of x
In this statement there is both hardware number and line number are
asterisk for default setting as only message is to be displayed here
Enter the value of x
This is very useful for messaging the user at run time.
Programming examples:
Lets see a complete example to find the simple interest using
Formatted I/O.

Prepared by Er. Om Prakash Mahato Page 20


OUTPUT:

11.5.3.1 Unformatted I/O:


FORTRAN 77 has also unformatted input/output statements i.e.
unformatted input/output statements do not need FORMAT
statements. In these statements, the computer itself decides the
format whereas in Formatted I/O statements, format is decided by
the programmer.
Unformatted I/O are :
1. READ * statement
2. PRINT * statement
1. Unformatted READ statement:
The general format of the statement is
READ *, list of variables
While running the program, in case of running this statement, cursor
blinks on the screen. The values must be typed by the users. If
there are more than one value, they must be separated by
commas. Also, it is noted that there must be an agreement in type
between each variables and the corresponding data supplied.
Character data must be enclosed in quotes.
Examples:
i) READ *,A,I,B
When this statement is encountered in a program, the
cursor blinks for data. The user must type three values of
which the first and third values must be real and second
one integer. They must be separated by commas. the
following can be given
4.8 , 151, 0.81
In this case, the values assigned are
A=4.8 I=151 B=0.81
ii) READ *, A,B,C

Prepared by Er. Om Prakash Mahato Page 21


Lets suppose A,B,C have to be same data then we can
type the data as
3*data e.g. 3*12.1233
Where 3 is called repetition factor.
Assigning the value, A,B and C becomes:
A=B=C=12.1233
2. Unformatted PRINT statement:
The general Format is
PRINT *,list of items
The list of item can include variables, constants, character strings
(enclosed in single quotes) and arithmetic expressions. The items
will be printed continuously with blank spaces between two
items. The number of items printed in one line varies from system
to system.
Examples:
Assume that the values A=5.1 , B=8.2
And consider the statement
PRINT *, A,B, A+B
The output will be as follows:
5.1 , 8.2, 13.3
Programming Example:
Lets see previous program (finding simple interest) by using
unformatted input/output statements.

OUTPUT

Prepared by Er. Om Prakash Mahato Page 22


11.6 Control Structures: GO TO, Logical IF, Arithmetic IF, Do loops
11.6.1 GOTO statement
There are two GO TO statements in FORTRAN programming which
are
1. Unconditional GO TO
2. Computed GO TO
1. Unconditional GO TO:
This statement is used to transfer the control to any other
statement unconditionally. The general form is
GO TO n
Where n is the statement number to which the control must
be transferred. The blank between GO and TO is optional i.e.
for example GOTO 75 and GO TO 75 both are accepted. Here,
75 is line number which can be before or below the GO TO
statement,
Consider the following example illustrating GOTO statement
10 WRITE(*,1)
1 FORMAT(HELLO WORLD)
GO TO 10
STOP
END

This program display the message


HELLO WORLD
Repeatedly. The program will never terminate i.e. makes
infinite loop.
2. Computed GO TO: the computed GO TO statement causes the
transfer of control depending upon the value of an integer
variable. The destination (where to go) is decided by the value
of the integer variable.
The general form is
GO TO (n1,n2,------nK),i
Where i is the integer variable

Prepared by Er. Om Prakash Mahato Page 23


And n1,n2,------nK are statement numbers.
This statement transfers the control to any one of the
statement n1, n2,..or nK depending on the value of i.
If i=1, the control is transferred to statement n1.
If i=2, the control is transferred to statement n2.
--------
If n=k, the control is transferred to statement n k. if i<1 or i>k
then this statement itself is ignored and the control goes to
the next line.
Some rules:
1. i must be single integer variable.
2. n1,n2,------nK are statement numbers occurring in any
order. Some of them be also equal.
3. The statement numbers must be enclosed within
parenthesis.
4. There must a comma between the closing parenthesis
and the integer variable.
A complete program to check odd or even using
Computed GOTO.

OUTPUT 1:

OUTPUT 2:

Prepared by Er. Om Prakash Mahato Page 24


11.6.2 IF statement
There are following structures of IF statement.
1. Arithmetic IF statement
This statement is used to transfer the program control
depending upon the value of an expression whether negative,
zero or positive.
The general form of Arithmetic IF statement is
IF(expression) n1,n2,n3
Where
(expression) is valid Fortran arithmetic expression enclosed
within parenthesis. n1,n2, n3 are statement numbers.
The value of the expression is evaluated first. If the value is
negative the control goes to statement number n1, if zero, it
goes to n2 and if it is positive, it goes to n3.
Lets see one most suitable complete programming example
using Arithmetic IF

Prepared by Er. Om Prakash Mahato Page 25


In above program, a character D is kept in the 6th columns
for continuation in the row 16th and 22nd as the 15th and 21st
lines are too long.
OUTPUT 1:

2. Logical IF statement
The logical if condition checks any given logical condition and
transfers the control accordingly.
The general format of the statement is
If(condition) statement
Where the condition is a logical condition, statement is an
executable statement. if the condition is true the statement is
executed and then goes to the next statement. if the
condition is false the control goes to the next statement.
Logical condition:
Relational operator Symbol used in FORTRAN
Less than(<) .LT.
Less than equal to(<=) .LE.
Greater than(>) .GT.
Greater than equal to(>=) .GE.
Equal to (=) .EQ.
Not equal to(!=) .NE.

A COMPLETE PROGRAM TO CHECK LEAP YEAR

Prepared by Er. Om Prakash Mahato Page 26


11.6.3 DO LOOPS
To execute a particular portion of programs number of times, we use
DO LOOPS in FORTRAN programming which is very important
tool.
The general form of the DO LOOPS is given by
DO n v=v1, v2, v3
..
..
n CONTINUE
where
n is used to reference the line having continue statement up to
which the loop block continues.
V is an integer variable called running variable of DO LOOP. V1 is
the initial value of running variable V. v2 is the final value of the
running variable V2. And v3 is the update value on initial value of
the running variable. V3 may be positive or negative. If v1>v2
then, v3 will be positive and if v1<v2 then , v3 will be negative. If
the update value is positive 1 then v3 is omitted.
How do loop works?
1. Firstly v is assigned by v1.
2. Then execute all the statements between DO and statement
n.
3. Increment v by v3. i.e. v=v+v3.
if v>v2, then stop otherwise to step 2.

Continue statement : this is a dummy statement which is used as the


last statement of a DO LOOP. It does not do any specific work. It just
indicates the end of the DO LOOP.

Examples:
Suppose we want to read 100 numbers and print all the numbers less
than 45. The following is the Do Loop needed.
DO 10 l=1,100

Prepared by Er. Om Prakash Mahato Page 27


READ(*,1)N
IF(N.GE.45) GO TO 10
WRITE(*,2)N
10 CONTINUE
----------------------------------
NESTED Do loops
When one Do loop is kept within another DO loop, then it is called a
Nested Do loop.
The general form of the Nested Do loop is as follows:
Do n1 k=v1,v2, v3
Do n2 L=x1,x2,x3
-------------------
--------------------
n2 continue
n1 continue
here, there two do loops one within another. For one value of k,
inner loop executes number of times based on L value<=x2, then k
is updated and again inner loop executes number of times. It goes
on till k becomes V2 then outer loop is also terminated. In this
way nested loop works.
Rules of Do loops:
1. It should be overlap. i.e. inner loop should be entirely within
outer loop.
2. In case of nested do loops, the same statements can be the
last statement for more than one Do loop.
For example:
Do 20 I=1,100
------------
-------------
Do 20 J=4,100,10
--------
-------
20 continue

Prepared by Er. Om Prakash Mahato Page 28


3. The running variable name of the outer Do loop should not be
used as running variable for an inner nested Do loop.
Lets see one complete example to find the sum of the series
1+x

11.7 Arrays: One dimensional and two dimensional


11.7.1 Introduction: When highly huge number of data are to be
processed like to add 100 data then it is difficult to declare 100
different variables. So, to make it easy, ARRAY concept raised in which
only single name like A is used for 100 numbers. The first number
must be referred as A(1), the second one as A(2) and So on

A(1) A(2) A(3) A(4)------------------------------------------- A(99)


In this case, we say that A is a subscripted variable. Sometimes we say
that A(I) is a subscripted variable and I is the subscript or index. If A is
a subscripted variable, A(1),A(2),A(3).A(n) is called an array. An
array is a sequence of consecutive memory locations.
Array may be of one or more than one dimensions.
Rules of subscripted variables:
The following rules may be strictly followed while defining the
subscripted variables.
1. The subscript is always taken as an integer. If we take decimal
values then only integer part is taken for consideration.
2. The subscript value cannot be negative.
3. The subscript must be given within parenthesis after the
variable name.
4. If there is more than one subscript, they are separated by
commas.
5.
11.7.2 Array Declaration
The general format of the array declaration is given by
DIMENSION variable (maximum value of subscripts)

Prepared by Er. Om Prakash Mahato Page 29


Note:- it is done to inform the computer about the size of array so
that the specified number of memory locations are reserved for that
array.
Example of array declaration:
1. For single dimensional array:
DIMENSION A(10)
This specifies that A is a one dimensional array with the
subscript varying from 1 to 10.
2. For two dimensional array:
DIMENSION M(3,4)
This specifies that M is a two dimensional array with first
subscript varying from 1 to 3 and second subscript from 1 to
4.
Instead of DIMENSION statement, the following statements
can also be used to declare the arrays
INTEGER
REAL
LOGICAL
CHARACTER
COMPLEX
For example, let see the following array declaration examples:
i) INTEGER A(10,10) : A is declared as an integer variable
with two subscripts. Both the subscript have 10 as the
maximum value.
ii) REAL A(10,10): A is declared as a real variable with two
subscripts. Both subscripts have 10 as maximum value.
iii) CHARACTER*20 A(10),N(100): A and N are declared as
CHARACTER variables with subscripts. A has maximum 10
items and N has 100 items. This means that A(1),
A(2),A(10), N(1), N(2),. N(100) are character
variables each holding 20 character positions.
11.7.3 Implied Do loops
1. Single dimensional implied loop: To read and print the single
dimensional array , we use the following code:

Prepared by Er. Om Prakash Mahato Page 30


Reading: Do 10 I=1, 100
READ(*,3) A(I)
3 FORMAT(_F10.2)
10 CONTINUE
PRINTING:
DO 20 I=1, 100
WRITE(*,6) A(I)
6 FORMAT(1X,F10.2)
20 CONTINUE
For reading and printing the array elements we have to write a Do
loop. FORTRAN also has the facility of reading or writing the entire
array with one statement. This statement is called the implied Do
loop.
For example, the following is the implied loop which reads the array A
of length 100.
READ(*,3)(A(I), I=1,100)
3 FORMAT(100 F10.2)
Similarly the following is the implied Do loop to print the elements of
the array
WRITE(*,6)(A(I), I=1,100)
6 FORMAT(100F10.2)
Lets see a complete program example: to find the sum of two single
dimensional arrays and print the resulted array.
C SUM THE CORRESPONDING ELEMENTS OF TWO 1D ARARYS
DIMENSION A(10),B(10),C(10)
WRITE(*,*)type the array element of A
READ(*,3) (A(I), I=1,10)
3 FORMAT(10F10.2)
WRITE(*,*)type the array element of B
READ(*,4) (A(I), I=1,10)
4 FORMAT(10F10.2)
Do 10 I=1,10
10 C(I)=A(I)+B(I)
WRITE(*,*)SUM IS

Prepared by Er. Om Prakash Mahato Page 31


WRITE(*,5)(C(I), I=1,10)
11 FORMAT(1x,10F10.2)
STOP
END

SOME MORE EXAMPLES FOR 1D ARRAY


1. Write a program to find the largest element of a given array.
c finding the largest element
DIMENSION A(100)
WRITE(*,*)'ENTER THE SIZE OF AN ARRAY'
READ(*,10)N
10 FORMAT(I5)

DO 1 I=1,N
PRINT *,'ENTER THE ARRAY ELELMENT ',I
READ(*,20)A(I)
20 FORMAT(F5.2)
1 CONTINUE
B=A(1)
DO 5 I=2,N
IF(B.LT.A(I)) B=A(I)
5 CONTINUE
WRITE(*,30)B
30 FORMAT('THE LARGEST ELEMENT OF GIVEN ARRAY IS: ',F5.2)
STOP
END
OUTPUT

Prepared by Er. Om Prakash Mahato Page 32


2. Implied loop for multidimensional array:
The implied Do loop can also be used for multidimensional arrays. For
example, consider a two dimensional array A(I,J) where I varies from 1
to 50 and J varies from 1 to 20. If we want to read all the elements of
the array, we write the nested Do loop as follows:
DO 20 I=1,50
DO 10 J=1,20
READ (*,2) A(I,J)
2 FORMAT(F10.3)
10 CONTINUE
20 CONTINUE
Sometimes we also write as follows:

DO 20 I=1, 50
DO 20 J=1, 50
20 READ(*,2) A(I,J)
2 FORMAT(F10.3)
But we can use the implied DO loop and write this as a single
statement as follows:
READ(*,2)((A(I,J),J=1,20),I=1,50)
It is noticed that the outer loop has I as running variable and
inner loop has J.
Examples:
The following two lines read and print a matrix of order MXN
READ(*,1)((A(I,J),J=1,N),I=1,M)
WRITE*,2)((A(I,J),J=1,N),I=1,M)
The typical example of a two dimensional array is matrix. Consider a
matrix
A= 1 2 3
4 5 6
7 8 9
This matrix has 3 rows and 4 columns. This is called as 34 matrix.
This can be represented by a subscripted variable with two subscripts.

Prepared by Er. Om Prakash Mahato Page 33


The first subscript varies 1 to 3 and second one from 1 to 4. This can
be defined by the statement
DIMENTSION A(3,4)
Reading and Printing a matrix
A matrix can be read by two nested loops. For example the matrix A
given above can be read using the following statements.
DO 20 I=1,3
DO 20 J=1,4
20 READ (*,6) A(I,J)
6 FORMAT (F10.3)
We can use implied Do loop and write as follows
READ(*,6) ((A(I,J), J=1,4), I=1,3)
In above program statement is executed we must input the entries
one below the other.
For better understanding,
Consider the program segment
READ (*,5) ((A(I,J), J=1,2) , I=1,2)
5 FORMAT(F10.2)
Suppose the matrix to be input is
1 7
9 2

During the execution of the program, we must input as


shown below.
1.0
7.0
9.0
2.0
It is possible to input the matrix in the matrix form. That is, in the
above example, it is possible to input as given below:
1.0 , 7.0
9.0 , 2.0
For inputting in this manner, the program segments must be
DO 10 I=1,2

Prepared by Er. Om Prakash Mahato Page 34


10 READ(*,5) (A(I,J), J=1,2)
5 FORMAT (2F10.2)
Here, in the READ statement the implied Do loop is used for reading
one complete row. For this READ statement , FORMAT is specified for
the complete row. Then ordinary Do loop is used to repeat for various
rows.
The following statement read a 45 matrix in matrix form
DO 10 I=1,4
READ(*,2) (A(I,J), J=1,5)
2 FORMAT(5F10.2)
10 CONTINUE
Similarly the segment for printing a matrix in the matrix form we write
as follows:
DO 20 I=1,4
WRITE(*,3) (A(I,J) , J=1,5)
3 FORMAT(5(3X,F10.2))
2O CONTINUE
The following is a small program to read a matrix and print it without
processing.
C A PROGRAM TO READ AND PRINT MATRIX
DIMENSION A(25,25)
WRITE(*,*) 'TYPE THE ORDER OF MATRIX M,N'
READ(*,2)M,N
2 FORMAT(2I5)
WRITE(*,*)'INPUT MATRIX IN MATRIX FORM'
DO 10 I=1,M
10 READ(*,4)(A(I,J) , J=1,N)
4 FORMAT(25F10.2)
WRITE(*,*)'THE MATRIX IS'
DO 20 I=1,M
20 WRITE(*,6)(A(I,J) , J=1,N)
6 FORMAT(25(3X,F10.2))
STOP
END

Prepared by Er. Om Prakash Mahato Page 35


OUTPUT

Program related to matrix


1. Finding sum/difference/product/division of corresponding
elements of two matrices
2. Finding the product of two matrices

1. Finding sum/difference/product/division of corresponding


elements of two matrices
Here, suppose two input matrices are A and B, and the
resulted matrix is C.
Then,
For Matrix addition,
C(I,J)=A(I,J)+B(I,J)
For Matrix subtraction
C(I,J)=A(I,J)-B(I,J)
For finding the product of the corresponding element of two
matrices
C(I,J)=A(I,J)*B(I,J)
For finding the division(quotient) of the corresponding
element of two matrices
C(I,J)=A(I,J)/B(I,J)
In all above cases, only operator is changed , all others are
same.
Let see complete program
C A PROGRAM TO FIND THE SUM OF TWO MATRICES
DIMENSION A(25,25),B(25,25),C(25,25)
WRITE(*,*) 'TYPE THE ORDER OF MATRIX A and B ,M,N'
READ(*,2)M,N
2 FORMAT(2I5)

Prepared by Er. Om Prakash Mahato Page 36


WRITE(*,*)'ENTER MATRIX A IN MATRIX FORM'
DO 10 I=1,M
10 READ(*,4)(A(I,J) , J=1,N)
4 FORMAT(25F10.2)
WRITE(*,*)'ENTER MATRIX B IN MATRIX FORM'
DO 15 I=1,M
15 READ(*,4)(B(I,J) , J=1,N)
DO 20 I=1,M
DO 20 J=1,N
C(I,J)=A(I,J)+B(I,J)
20 CONTINUE

WRITE(*,*)'THE RESULTED MATRIX C IS'


DO 25 I=1,M
25 WRITE(*,6)(C(I,J) , J=1,N)
6 FORMAT(25(3X,F10.2))
STOP
END
OUTPUT

2. MATRIX MULTIPLICATION
C A PROGRAM TO FIND THE PRODUCT OF TWO MATRICES
DIMENSION A(25,25),B(25,25),C(25,25)
WRITE(*,*) 'ENTER THE ORDER OF MATRIX A '
READ(*,2)M1,N1
2 FORMAT(2I5)
WRITE(*,*) 'ENTER THE ORDER OF MATRIX B '

Prepared by Er. Om Prakash Mahato Page 37


READ(*,2)M2,N2
IF(N1.NE.M2)GO TO 50

WRITE(*,*)'ENTER MATRIX A IN MATRIX FORM'


DO 10 I=1,M1
10 READ(*,4)(A(I,J) , J=1,N1)
4 FORMAT(25F10.2)
WRITE(*,*)'ENTER MATRIX B IN MATRIX FORM'
DO 15 I=1,M2
15 READ(*,4)(B(I,J) , J=1,N2)
DO 20 I=1,M1
DO 20 J=1,N2
C(I,J)=0
DO 30 K=1,N1
30 C(I,J)=C(I,J)+A(I,K)*B(K,J)
20 CONTINUE

WRITE(*,*)'THE RESULTED MATRIX C IS'


DO 25 I=1,M1
25 WRITE(*,6)(C(I,J) , J=1,N2)
6 FORMAT(25(3X,F10.2))
STOP
50 PRINT *, 'MATRIX SIZE IS MISMATCHED'
STOP
END
OUTPUT

Prepared by Er. Om Prakash Mahato Page 38

You might also like