You are on page 1of 15

Fortan

-Formula Translator -1977

-Fortran (/ˈfɔːrtræn/; formerly FORTRAN) is a general-purpose, compiled imperative programming language that is
especially suited to numeric computation and scientific computing.
We will study FORTRAN with reference to standard of FORTRAN 77

Constants,Variables and datatypes

 Integer, byte, real, double precision, complex, character, character*N

Character set
Uppercase: A-Z
Lowercase: a-z
Digits: 0-9

Special Character:
Blank = Equal + Plus - Minus * Asterisk /Slash ()parenthesis
, Comma . Decimal point $ Currency symbol ' Apostrophe : Colon
! Exclamation point _ Underscore “Quotation mark

Escape Sequence

Escape Character
Sequence

\n Newline

\t Tab

\b Backspace

\f Form feed

\v Vertical tab

\0 Null

\' Apostrophe, which does not terminate a string

\" Quotation mark, which does not terminate a string

\\ \ or slash

\x x, where x is any other character


Data Types Constants and Variables INTEGER
positive and negative integral numbers and zero, size 4 bytes
REAL
positive and negative numbers with a fractional part and zero, size 4 bytes
DOUBLE PRECISION
Same as REAL but using twice the storage space.
COMPLEX
Ordered pair of REAL data: real and imaginary components
LOGICAL
Boolean data representing true or false
CHARACTER
Character strings

Operators

1. Arithmetic operators

Priority Operation Symbol FORTRAN 77 Arithmetic


Expression Expression

right to
left Exponentiation ** A**B b
A

left to Multiplication and * A*B a×b


right Division / A/B a÷b

left to Addition, Subtraction, + A+B a+b


right and - A-B a-b
Unary Minus -A -a

Note: Parentheses have the highest priority and can be used to force lower priority calculations to occur before
higher priority ones.

n
The arithmetic expression -a +b×c-d÷e is written in FORTRAN 77 as -A**N+B*C-D/E and is evaluated in the
following order:

i. A**N
ii. B*C
iii. D/E
iv. - in front of A**N
v. + between -A**N and B*C
vi. - between B*C and D/E
Logical Operators
Three operations can be performed on LOGICAL variables. The truth table below sums this up:

A B .NOT. A A .AND. B A .OR. B

.TRUE. .TRUE. .FALSE. .TRUE. .TRUE.


.TRUE. .FALSE. .FALSE. .FALSE. .TRUE.

.FALSE. .TRUE. .TRUE. .FALSE. .TRUE.

.FALSE. .FALSE. .TRUE. .FALSE. .FALSE.

Relational Operators

Operator Meaning Fortran Expression Result assuming


a=5 and b=3

.EQ. equal to a.EQ.b 0

.NE. not equal to a.NE.b 1

.LT. less than a.LT.b 0

.LE. less than or equal to a.LE.b 0

.GT. greater than a.GT.b 1

.GE. greater than or equal to a.GE.b 1

Assignment operators
Variable_name= expression; y= x+5

String concatenation
Double backward slash ‘//’ is used for string concatenation.

FORTRAN 77 program format

It not a free format language i.e. it has very strict set of rules for writing a program.
Structure of FORTRAN 77 program
Program name
Declarations
Statements
End
Column 1 Blank, or The character c or * means that this line contains only comment.
Column 2-5 Blank, or Statement Label.
Column 7-72 Statements, all information after column 72 is ignored.
Column 6 Only one statement per line is permitted, If a statement does not fit on one line, it can be
continued on the next line(s). On position 6 of the next line(s) there has to be a ‘+’ symbol.

1 2-5 6 7-72
c Hello world program in FORTRAN
program Hello
100 write(*,*) ‘hello from console’
+ ‘this is for multiple statements’
end program Hello
Program Output
program hello Hello World
write(*,*)'Hello World'
end program hello
Sum of two Numbers
c sum of 2 numbers in FORTRAN
program sum INTEGER enter value of a and b 7
a,b,sum 8
write(*,*)'enter value of a and b' the sum is 15
read(*,*)a,b
sum=a+b
write(*,*)'the sum is',sum end
program sum

Sum of two Complex numbers


program complex_number enter first complex number (4,6)
complex a,b,sum enter second complex number (8,6)
write(*,*)'enter first complex number' the sum is ( 12.0000000 , 12.0000000 )
read(*,*)a
write(*,*)'enter second complex number'
read(*,*)b
sum=a+b;
write(*,*)'the sum is',sum
end program complex_number

Concatenate two strings


program concat_string character*20 enter first
str1,str2,str write(*,*)'enter first string
string' read(*,*)str1 Cristiano
write(*,*)'enter second string' enter second
read(*,*)str2 string Ronaldo
write(*,*)'Concatenated string is:', str1//str2 Concatenated string is:Cristiano Ronaldo
end program concat_string

Formatted and Unformatted IO operations

 read() and write() function can be used in conjunction with format specifier.

 The general syntax of read() and write() are as follows:

read(unit#,format#) list of arguments


write(unit#,format#) list of arguments

 unit is a number, which has an association with a particular device.


 In General Unit=5 is reserved for input from the keyboard & Unit=6 for output to console.
 If * is used in place of unit# & format#, default input/output device & default format is assumed.
Format specifications:
label FORMAT (format_specifications)
I Format
Used for formatting integer variable or constant Syntax: Iw,
where w is the width of the integer data

F Format
Used for formatting real variables.
Syntax: Fw.d, where w is the total width & d is precision of real data.
F4.2 read 6743 as 67.43
F4.1 read 6743 as 674.3
F4.0 read 6743 as 6743
X Format:

Used for formatting character or string data Syntax: nX,


skips n columns.

E Format
Used for formatting real numbers in exponential form.
Syntax: Ew.d, where w is total field width including exponent and d is the decimal width or
precision of the exponent.
2
E6.0 would read 1234E2 as 1234x10 123400
2
E6.1 would read 123.4E2 as 123.4x10 12340

T Format: th
Used for starting output from n column.
Syntax: nT, where n is the column number from which output starts.
Illustrate concept of Format specifiers
program formatting
integer x,y
real r enter any 2 decimal numbers 200
write(6,*)'enter any 2 decimal numbers' 2
123
read(5,100)x,y data according to format 100: 200
write(6,*)'enter an real number' 2
read(5,300)r data according to format 300: 1.23
100 format(I4,I2) data according to format 200:
write(6,*)'data according to format 100:' 200 2
write(6,100)x,y
write(6,*)'data according to format 300:'
write(6,300)r
write(6,*)'data according to format 200:'
write(6,200)x,y
200 format(I4,4x,I2)
300 format(F4.2)
c 4x leaves 4 space before printing value of y end
program formatting

Example:
program precedenceOp
! this program checks logical operators
integer a, b, c, d, e
! assigning values
a = 20
b = 10
c = 15
d = 5
e = (a + b) * c / d ! ( 30 * 15 ) / 5
print *, "Value of (a + b) * c / d is : ", e
e = ((a + b) * c) / d ! (30 * 15 ) / 5
print *, "Value of ((a + b) * c) / d is : ", e
e = (a + b) * (c / d); ! (30) * (15/5)
print *, "Value of (a + b) * (c / d) is : ", e
e = a + (b * c) / d; ! 20 + (150/5)
print *, "Value of a + (b * c) / d is : " , e
end program precedenceOp

Output:
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50
Control structures
GOTO:
Used to jump from instruction from one location to other.

Unconditional goto
Syntax:
goto label
e.g.,
program unconditionalgoto
a=5
1 write(*,*)a

goto 1
end program unconditionalgoto

Computed goto
Syntax:
Goto (s1,s2,s3,….,sn) , integer expression

 when value of integer expression is equal to 1, control is transferred to statement number 1, if 2 then
transferred to s2 and so on.

 If the value of integer expression is negative or greater than n then the goto statement is ignored.

program computedgoto
a=5
b=6
n=2
goto (1,2,3),n
1 write(*,*)a+b
2 write(*,*)a-b
3 write(*,*)a*b
End

If and goto statement:

The correct way is to use if and goto:

label if (logical expr) then


statements
goto label
endif

or,
label continue
statements
if (logical expr) goto label

Here is an example that calculates and prints all the powers of two that are less than or equal to 100:
program fa
integer n
n=1
10 if (n .le. 100) then
write (*,*) n
n = 2*n
goto 10
endif
end program fa

Arithmetic If :
(Deleted and Obsolescent Language Features )
Arithmetic IF
Syntax: IF(expression) s1,s2,s3
First of all the value of expression is evaluated.
If Value of expression is –ve, program branches to statement s1.
If value is Zero branches to s2
If value is +ve it branches to s3

The IF statement transfers control to the first, second, or third label if the value of the arithmetic expression is less than
zero, equal to zero, or greater than zero, respectively.
program arithmeticif
a=5
b=6
d=0
if(d) 1,2,3
1 write(*,*)a+b
goto 4
2 write(*,*)a-b
goto 4
3 write(*,*)a*b
4 end

e.g.,
program quadratic
integer a,b,c,d
read(*,*)a,b,c
d=b**2-4*a*c
write(*,*)d
if(d) 1,2,3
1 print*,'Root is negative' STOP
2 print*,'Root is equal' STOP
3 print*,'Root is positive' STOP
End
Logical IF
Syntax: IF(expression)
THEN Statement(s)
END IF

program ifProg
integer a = 10
if (a < 20 ) then
print*, "a is less than 20"
end if
print*, "value of a is ", a
end program ifProg

IF ELSE
IF(expression) THEN True block
of Statement(s) ELSE
False block of Statement (s)
ENDIF
program logicalOp
! this program checks logical operators
logical a, b
a = .true.
b = .false.
if (a .and. b) then
print *, "Line 1 - Condition is true"
else
print *, "Line 1 - Condition is false"
end if
if (a .or. b) then
print *, "Line 2 - Condition is true"
else
print *, "Line 2 - Condition is false"
end if
! changing values
a = .false.
b = .true.
if (.not.(a .and. b)) then
print *, "Line 3 - Condition is true"
else
print *, "Line 3 - Condition is false"
end if
if (b .neqv. a) then
print *, "Line 4 - Condition is true"
else
print *, "Line 4 - Condition is false"
end if
if (b .eqv. a) then
print *, "Line 5 - Condition is true"
else
print *, "Line 5 - Condition is false"
end if
end program logicalOp
ELSE IF Ladder

IF (expression 1) THEN
Statement 1
ELSE IF (expression2) THEN
Statement 2
………….
ELSE IF (expression n) THEN Statement n
ELSE
Default statement END
IF

program ifElseIfElseProg
integer a
a= 100
! check the logical condition using if statement
if( a == 10 ) then
! if condition is true then print the following
print*, "Value of a is 10"
else if( a == 20 ) then
! if else if condition is true
print*, "Value of a is 20"
else if( a == 30 ) then
! if else if condition is true
print*, "Value of a is 30"
else
! if none of the conditions is true
print*, "None of the values is matching"
end if
print*, "exact value of a is ", a
end program ifElseIfElseProg

DO LOOP
Syntax:
do label, integer_varaible = initial_value, final_value, step_size block of
statement(s)
label continue

Alternative syntax:
do integer_variable =initial_value, Final_value, Step_size blocke of
statement(s)
enddo
Example:
program test
a=5
do 123,i=1,5,2
print*,a
123 continue
End

Or

Program domtest
a=5
do i=1,5,2
print*,a
enddo
end

Prime or Composite in FORTRAN


program prime integer
n,i enter a number
write(*,*)'enter a number' 23
read(*,*)n prime number
do 200, i=2,n-1,1
enter a number
if (mod(n,i).eq.0) THEN
22
write(*,*)'composite number'
composite number
goto 201
end if
200 continue
write(*,*)'prime number'
201 stop
end program prime

Factorial program:
program fac
! compute factorials
nfact = 1
do n = 1, 10
nfact = nfact * n
! printing the value of n and its factorial
print*, n, " ", nfact
end do
end program fac
Output: 1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800

program printNum
integer n
do n = 11, 20
! printing the value of n
print*, n
end do
end program printNum
Output:
11
12
13
14
15
16
17
18
19
20

This program calculates the factorials of numbers 1 to 10:


program factorial
integer :: nfact = 1
integer :: n
! compute factorials
do n = 1, 10
nfact = nfact * n
! print values
print*, n, " ", nfact
end do
end program factorial

Output
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800

do-while Loop
It repeats a statement or a group of statements while a given condition is true. It tests
the condition before executing the loop body.
Syntax
do while (logical expr)
statements
end do

or alternatively,
while (logical expr) do
statements
enddo
The program will alternate testing the condition and executing the statements in the body as long as the condition in
the whilestatement is true.

program factorial
integer :: nfact = 1
integer :: n = 1
do while (n <= 10)
nfact = nfact * n
n = n + 1
print*, n, " ", nfact
end do
end program factorial

program nestedLoop
implicit none
integer:: i, j, k
iloop: do i = 1, 3
jloop: do j = 1, 3
kloop: do k = 1, 3
print*, "(i, j, k): ", i, j, k
end do kloop
end do jloop
end do iloop
end program nestedLoop

Output:
(i, j, k): 1 1 1
(i, j, k): 1 1 2
(i, j, k): 1 1 3
(i, j, k): 1 2 1
(i, j, k): 1 2 2
(i, j, k): 1 2 3
(i, j, k): 1 3 1
(i, j, k): 1 3 2
(i, j, k): 1 3 3
(i, j, k): 2 1 1
(i, j, k): 2 1 2
(i, j, k): 2 1 3
(i, j, k): 2 2 1
(i, j, k): 2 2 2
(i, j, k): 2 2 3
(i, j, k): 2 3 1
(i, j, k): 2 3 2
(i, j, k): 2 3 3
(i, j, k): 3 1 1
(i, j, k): 3 1 2
(i, j, k): 3 1 3
(i, j, k): 3 2 1
(i, j, k): 3 2 2
(i, j, k): 3 2 3
(i, j, k): 3 3 1
(i, j, k): 3 3 2
(i, j, k): 3 3 3
Array in FORTRAN
Declaration and Initialization of 1 D array and 2 D array
Syntax (1D):
data_type array_name (size) integer a(50)
Fifty integers naming a(1), a(2),… .............................,a(50)

Syntax (2D):
data_type array_name (row_size, column_size) integer a(3,3)
Nine integer elements declared as: a(1,1), a(1,2), a(1,3),a(2,1), a(2,2), a(2,3),a(3,1), a(3,2), a(3,3)

Initialization by using data statement


data variable list /value list/
Integer a(4)
data a(1),a(2),a(3),a(4) / 11,12,13,14/
Initialization by using implied do loop Syntax:
Array_name(counter_variable), counter_variable = initial_value, final_value, step_size
integer a(50) read(*,*) a(i),
i=1,50,1
Alternatively it can be done as:
do 100 i=1,50,1
read(*,*) a(i)
100 continue

Example:
program test
integer a(5),sum,k
sum=0
do 1 k=1,5,1
read(*,*) a(k)
1 CONTINUE
do 2 k=1,5,1
sum=sum+a(k)
2 CONTINUE
write(*,*)SUM
end

Example:
program implieddoloop
integer a(5),sum,k sum=0
read(*,*)(a(k),k=1,5)
do 2 k=1,5,1 sum=sum+a(k)
2 CONTINUE
write(*,*)SUM
end
Sorting numbers in ascending order
program sorting enter number of terms
integer a(100),n,i,j,temp 5
write(*,*)'enter number of terms' enter numbers 5
read(*,*)n 10
write(*,*)'enter numbers' do 7
100 i=1,n,1 read(*,*)a(i) 15
100 continue 8
do 200, i=1,n,1 the entered number in ascending order:
do 300, j=i+1,n,1
5 7 8 10 15
if (a(i).ge.a(j))then
temp=a(i)
a(i)=a(j)
a(j)=temp
endif
300 continue
200 continue
write(*,*)'the entered number in ascending order:'
write(*,*)(a(i),i=1,n)
end program sorting

2d Array

program implieddoloop
integer a(10,10),b(10,10),r1,c1,r2,c2
do 1 i=1,2,1
do 2 j=1,2,1
read(*,*)a(i,j)
2 continue
1 continue
do 3 i=1,2,1
do 4 j=1,2,1
write(*,*)a(i,j)
4 continue
3 continue
end

You might also like