0% found this document useful (0 votes)
21 views9 pages

Fortran Lecture 4

Uploaded by

Manozer Mensah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views9 pages

Fortran Lecture 4

Uploaded by

Manozer Mensah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Example program 1/5

Product of two variables


PROGRAM firstprogram ! start of program
!This program finds product of two real variables
IMPLICIT NONE ! All variables used must be declared
REAL :: x,y,z ! Declare variables
x = 5.1 ! Assign x
y = -17.2 ! Assign y
z = x*y ! Assign (x*y) to z
PRINT*,’The product of x and y is’, z ! Print out z
END PROGRAM firstprogram ! end of program
Example program 2/5

Radius and perimeter of cycle

PROGRAM maths ! start of program


!This program finds the radius and perimeter of a cycle
IMPLICIT NONE ! All variables used must be declared
REAL, PARAMETER :: pi = 3.143 ! parameter declaration
REAL :: rad, area, peri ! Declare variables to be used
PRINT*, ’Enter the radius of the cycle’
READ*, rad
area = pi*rad**2 ! assign area
peri = 2*pi*rad ! assign peri
PRINT*,’The area of a cycle of radius’, rad ’is’, area
PRINT*, ! print blank line
PRINT*,’The perimeter of a cycle of radius’, rad ’is’, peri
END PROGRAM maths ! end of program
Formatted Output 1/7
Lots of input/output ( I/O) related features in Fortran - not all will
be considered for now
Fortran PRINT statements do formatting by preceding the variables
to be printed with (format descriptor).
Format descriptor are put in parentheses.
Symbols used with format descriptor are shown in the table below:
Symbol meaning
c Column number
d Number of digits to right of the decimal places of real
m Minimum number of digits to be displayed
n Number of spaces to skip
r Repeat count - the number of times to use a descriptor
w Field width - the number of characters to use including
leading (+ or -) signs as well as decimal point

They are applied to both output and input


Formatted Output 2/7

Integer Output - The I Descriptor

Integer values are right justified in their fields. This means that integers
are printed out so that the last digit of the integer occupies the
rightmost column of the field.
The I descriptor has the general form
rIw or rIw.m
where r, w, and m have the meanings given in the above table.
Example
INTEGER :: x = 12345
PRINT’(I5)’, x !integer with field width = 5, 12345 is the output
If the integer is too large to fit into the field in which it is to be printed,
then the field is filled with asterisk.
PRINT’(I3)’, x !integer with field width = 3, **** is the output
Formatted Output 3/7

Real Output - The F Descriptor


Real values are printed right justified within the fields. If the specified
field width is less than the number to be printed, the number is rounded
off before it is displayed. On the other hand, if the specified width is
more than the number to printed, extra zeros are appended to the right
of the decimal point.
The I descriptor has the general form
rFw.d
where r, w, and d have the meanings given in the table.
Example:
REAL :: x = 1234.678
PRINT’(F8.3)’, x !output = 1234.678
PRINT’(F8.2)’, x !output = 1234.68 why difference in position?
PRINT’(F8.4)’, x !output = *****, why?
Formatted Output 4/7

Real Output - The E Descriptor

Real data can be printed in exponential form using the E descriptor. Real
values displayed by E descriptor are normalized to a range between 0.1
and 1.0. For example 4.096 x 103 will appears as 0.4096E+04 using the
E descriptor
The E descriptor has the general form
rEw.d
The width w of E descriptor must satisfied the expression
w ≥ d +6 where d has the same meaning given in the table
Example:
REAL :: res = 23456
PRINT’(E11.5)’, res !output = 0.23456E+05
PRINT’(E13.5)’, res !output = 0.23456E+05, check the position
PRINT’(E8.2)’, res !output = 0.23E+05
Formatted Output 5/7

True Scientific Notation - The ES Descriptor

The ES descriptor is exactly the same as the E descriptor, except that


the number to be printed is displayed with mantissa in the range between
1 and 10
The ES has the form:
rESw.d
The formula for the minimum width of an ES descriptor is the same as
that of E but the leading zero in E descriptor is replaced by a significant
digit in ES.
Example:
REAL :: ans = 2345.6
PRINT’(ES11.5)’, ans ! output = 2.34560E+03
PRINT’(ES8.2)’, ans ! output = 2.35E+03
PRINT’(ES7.2)’, ans ! output = *****, why?
Formatted Output 6/7

Character Output - The A Descriptor

A descriptor has the form:


rA or rAw
where r and w have the meanings given in the table
The rA descriptor displays character data with the same width as the
number of characters being displayed whiles the rAw displays character
data with width w. The rA are printed right justified and if the width of
the field is shorter than the length of character variables only first w
characters of the variable are printed.
Example:
CHARACTER(LEN = 7) :: dept = ’physics’
PRINT’(A7)’, dept ! output = physics
PRINT’(A)’, dept ! output = physics
PRINT’(A3)’, dept ! output = phy
PRINT’(A3)’, dept(5:7) ! output = ics ,you should know why?
Formatted Output 7/7

Horizontal Spacing - The X Descriptor

The X Descriptor is used to add one or more blanks space between two
values on the output line.
The X Descriptor has the form of a positive integer followed by X:
nX
where n is the number of blanks to insert.
Example:
CHARACTER(LEN = 7) :: dept = ’physics’
REAL :: ans = 2345.6
PRINT’(ES8.2,A7)’, ans, dept ! output = 2.35E+03physics
PRINT’(ES8.2,2X,A7)’, ans, dept ! output = 2.35E+03 physics
where 2X create two blank spaces between ES8.2 and A7 outputs

You might also like