Sunteți pe pagina 1din 29

Fortran77

Tutorial
By Shubham.

Fundamentals of Program solving

1.
Algorith
m
2. Flow
Chart
3.
Program

Various
Various sequential
sequential elementary
elementary steps
steps of
of problem
problem
written
in
simple
English
written in simple English
Also
Also called
called pseudo-code
pseudo-code of
of the
the problem
problem

Graphical
Graphical representation
representation of
of the
the algorithmic
algorithmic
structure
structure of
of the
the problem
problem
Flow
Flow of
of logic
logic of
of the
the problem
problem

Various
Various sequential
sequential steps
steps written
written in
in High
High Level
Level
Language
Language e.g.
e.g. Fortran,
Fortran, C,
C, C++
C++
Script
Script usually
usually combination
combination of
of numeric
numeric and
and
alphabetic
keys
with
special
characters
alphabetic keys with special characters having
having
special
meanings
special meanings

Examples of
Algorithms
Velocity-time relation
Step 1. Give initial velocity u,time t

and acceleration a.
Step 2. Find final velocity v using the
relation v=u+a*t
Step 3. Print final velocity.
Step 4. End of the program.

Finding Maximum number


Step 1. Feed total number of points n.
Step 2. Give first data point x.
Step 3. initialize a variable max=x.
Step 4. Start a loop over I varying from

2 to n in steps of 1.
Step 5. Give next data point y.
Step 6. If y>max, then set max=y.
Step 7. End of loop started at step 4.
Step 8. Print max.
Step 9. End of the program.

Examples of flow charts


Velocity-time relation
Start
Start

Read n,x

Read
u,t,a

Max=x

V=u+a*t

Print v

End

Do 5 i=2,n
Read y
Is
y>ma
x

Max=y

5
Write
max

Finding end
maximum of numbers

Program writting
Introduction to FORTRAN 77
An abbreviation for FORmula TRANslation
Has a sequential structure executed from top to bottom.
1. Input

2.Processing of
data

3. Output

4. Termination

1. INPUT

Allows one to feed


values of the variables
in the natural format

Syntax

read(*,*) variable1, variable2


Example
read(*,*) a,b,n,max,pi

Reads data
fed from key
board

Types of
variables

Intege
r i,j,k,l,m,n

Real
a-h,o-z

2. Processing of data
Different statements used in Program writing:
Input statement
Type declaration statements
Assignment statement
Arithmetic operation statements
Library function statements
Selective structure
(a) go to statement (b) Arithmetic if statement
(c)relational expressions (d) logical expressions
(e)logical if statement (f) block if statement
(g) nested block if (h) multi-alternative block if
7. Repetitive structure
(a) if-go to (b) do- continue loop
(c) nested do loop (d) arrays and dimension
1.
2.
3.
4.
5.
6.

2.Type declaration Statements


Syntax
integer list of variables
real list of variables
Example
c
"Program to find sum of two numbers"
real sum, x, y
write(*,*)'Enter the given numbers'
read(*,*)x,y
sum=x+y
write(*,*)sum of given numbers =',sum
stop
end

3.Assignment statement
Syntax
Variable=value
Example
c Program to find area of a circle
real pi, area
write(*,*)enter radius
read(*,*) r
pi=3.1432
area=pi*r**2
write(*,*)area of circle =,area
stop
end

4.Arithmetic operation statements


+Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation
Hierarchy Order
61+23*2**2

**

6.5+2.3*3.0/4.1**2

/,*
(left
to
right)
6.5+2.3*3.0/4.1**2
125/5*5+2
+ , - (left to right)
6.5+2.3*3.0-4.1**2

6*2-1+2*3

5.Library Functions
Formula transformation
Arithmatic
Functions
sqrt(x) real(x)
abs(x)
alog(x)
iabs(n)
alog10(x)
int(x)
exp(x)
nint(x)
mod(n,m)
anint(x)
amod(x,y)
float(n)

Trignometric
Functions

sin(x)
cos(x)
tan(x)
asin(x)
acos(x)
atan(x)

Hyperbolic
Functions

sinh(x)
cosh(x)
tanh(x)

Example
C

Program to find area of a triangle


real area
write(*,*)enter sides of triangle
read(*,*)a,b,c
s=(a+b+c)/2.0
area=sqrt(s*(s-a)*(s-b)*(s-c))
write(*,*)area of the triangle = ,area
stop
end

6.Selective
structure
(a) Go To Statement
(b) Arithmetic If Statement
(c) Relational Expressions
(d) Logical Expressions
(e) Logical If Statement
(f) Block If Statement
(g) Nested Block If Statement
(h) Multi-alternative Block If Statement

(a) Go To Statement
Performs unconditional transfer of control
Used to jump from one part of a program to another
Execution will continue indefinitely
Ctrl+C keys or Ctrl+Break keys for termination
Syntax

go to n
(where n is a statement label between 1 to 99999)
Example
20 read(*,*)a,b,c
sum=a+b+c
write(*,*)a,b,c,sum
go to 20
stop
end

(b) Arithmetic If
Statement
Syntax

if(Arithmetic expression ) n1,n2,n3


If the numeric value of the Arithmetic expression is less than 0,computer transfers the control to
statement n1,
if it is equal to 0,control is transferred to statement n2,
if it is greater than 0,control is transferred to n3
Example:
c Program to discuss nature of roots of a quadratic eqn
write(*,*)enter coefficients of the equation
read(*,*)a,b,c
if(b**2-(4*a*c)) 30,40,50
30 write(*,*)roots are imaginary
go to 90
40 write(*,*)roots are real and equal
go to 90
50 write(*,*)roots are real and distinct
90 stop
end

(c) Relational Expressions


Used for conditional control

Example of Relational Expression


Example:
c
"Program to generate odd numbers"
real l,u
write(*,*)'enter lower limit and upper limit'
read(*,*) l,u
write(*,*)'List of odd numbers is:'
do 20 i=l,u
y=i/2.0
if(int(y).ne.y) write(*,*) i
20
continue
stop
end

(d) Logical Expressions


If conditions are complex,then relational

expressions can be joined using the logical


operators to implement them.

Example: (age.gt.5).and.(age.lt.60)

(e) Logical If Statement


This statement checks the logical expression.
Syntax

if(logical expression) statement 1


statement 2
If the logical expression is TRUE,it executes the statement 1,then the statement 2. If
it is FALSE,it executes statement 2 skipping the statement 1.

Example: c Finding Discount on Sales


read(*,*) sales
disc= 0.05
if(sales.gt.100.and.sales.lt.500) disc= 0.1
if(sales.ge.5000) disc= 0.15
totdis= sales*disc
write(*,*)sales,totdis
stop
end

(f) Block If Statement


It is used when execution of more than one statement

depends upon certain conditions.


Syntax:
if(logical expression) then
statement 1
: :
statement n
else
statement a
: :
statement m
endif

(g) Nested Block If


Statement
One Block If statement may contain another Block If statement.
The inner Block If statement should never cross the If-Then or Else

Blocks.
Example
read(*,*) x
if(x.lt.5) then
y= sin(x)
else
if(x.eq.5) then
y= cos(x)
else
y= tan(x)
endif
endif
write(*,*) y
stop
end

(h) Multi-alternative Block If


statement
A better form of nested blocks can be performed using multi-

alternative block If statements as shown below:


if(logical expression 1) then
statement 1
: :
statement n
else if(logical expression 2) then
statement a
: :
statement m
else
statement A
: :
statement M
endif

7. Repetitive
Structure

(a) If- Go To Statements


It employs a counter variable in conjuction with the If

and Go To statements.
Example
c factorial of a positive integer
integer fact
read(*,*) n
m=1
fact=1
10 fact=fact*m
m=m+1
if(m.le.n) go to 10
write(*,*)n, fact
stop
end

(b) Do-Continue Loop Statements


More convenient way
Example

c double factorial
read(*,*) n
fact=1
do 10 i=1,n,2
fact=fact*i
10 continue
write(*,*) n, fact
stop
end
The no. of times a loop get executed =

(c) Nested Do Loops


One Do loop may contain another Do loop.
The inner Do loop can never cross the outer loop.
Example

c Generating multiplication tables upto 10x10


integer prod
do 20 i=1,10
do 20 k=1,10
prod=i*k
write(*,*) i, x, k, = , prod
20 continue
stop
end

3. OUTPUT
Results of the processing part are displayed here.
The various write statements written in the program

are the output statements.


Example:
C Program to find area of a triangle

real area

write(*,*)enter sides of triangle

read(*,*)a,b,c

s=(a+b+c)/2.0

area=sqrt(s*(s-a)*(s-b)*(s-c))

write(*,*)area of the triangle = ,area

stop

end

4. Termination
This brings to the end of execution of a

program.
STOP and END statements are the termination
statements.

Thanks.