0% found this document useful (0 votes)
79 views

Fortran

Here is a simple Fortran program to display "Hello World!": Program hello write(*,*) 'Hello World!' End program hello This program contains: - The program statement with the program name "hello" - A write statement using asterisk (*) format specifier to display the text "Hello World!" to the standard output - The end program statement to mark the end of the main program When executed, this program will display the text "Hello World!" on the standard output. This demonstrates a basic Fortran "Hello World!" program structure and output statement.

Uploaded by

Lokendra Joshi
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)
79 views

Fortran

Here is a simple Fortran program to display "Hello World!": Program hello write(*,*) 'Hello World!' End program hello This program contains: - The program statement with the program name "hello" - A write statement using asterisk (*) format specifier to display the text "Hello World!" to the standard output - The end program statement to mark the end of the main program When executed, this program will display the text "Hello World!" on the standard output. This demonstrates a basic Fortran "Hello World!" program structure and output statement.

Uploaded by

Lokendra Joshi
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/ 73

CHAPTER-11

FORTRAN (8 hours)

1.Character Set
2.Data types, Constants and variables
3.Arithmetic operations, Library functions
4.Structure of a Fortran Program
5.Formatted and Unformatted Input / Output Statements
6.Control Structures: Goto, Logical If, Arithmetic If, Do loops
7.Arrays: one dimensional and two dimensional

FORTRAN NOTES 1
Introduction
▪ ForTran stands for Formula Translation.
▪ It is a high level programming language that is being used by scientists and
engineers for the application that includes complex scientific and numerical
calculations.
▪ The idea for the development of FORTRAN was conceived by John W. Backus around
1953 at IBM Company.
▪ The main purpose behind the development of the original version of FORTRAN was
to develop a more practical alternative to assembly language for programming IBM
704 Mainframe computer.
▪ A draft specification for the IBM Mathematical Formula Translating System was
completed by mid 1954.
▪ The first manual for FORTRAN appeared in October 1956, with first FORTRAN
compiler delivered in April 1957.
FORTRAN NOTES 2
Introduction
▪ Since then several versions of FORTRAN appeared in the scene with some additional
features and improvements over the previous version.
▪ Following table briefly summarize the FORTRAN versions and corresponding release date:

▪ As far as this course is concerned, we shall study FORTRAN with reference to the standards of
FORTRAN 77.
FORTRAN NOTES 3
Character Set
• Following are the different character set supported by FORTRAN:
Uppercase: A to Z
Lower Case: a to z
Digits: 0 to 9
Special characters:
Blank, equal(=), plus(+), minus(-), asterisk(*), slash(/), left parenthesis ( , right
parenthesis ),
comma(,), decimal point(.), Dollor sign($), apostrophe(’) , colon(:) ,
exclamation(!),
underscore( _ ), double quote(”)

FORTRAN NOTES 4
Character Set

FORTRAN NOTES 5
Data Types, Constants & Variables:
• The central concept of data type in FORTRAN is similar to that in C.
• The data type of a variable or constant must be defined according to the data that
are to be stored in the variable or constant being defined.
• Following are the different data types available in FORTRAN:-
▪ Integer:
• It can be used to store positive and negative numbers.
• It occupies 4 bytes of memory.
• It can represent any values in the range -231 to +231
▪ Real:
• It can be used to store positive and negative fractional number.
• It occupies 4 bytes of memory.
• It can store values in the range from 1.2 × 10-38 to 3.4 × 1038
FORTRAN NOTES 6
Data Types, Constants & Variables:
▪ Double Precision:
• It can store positive and negative fractional number.
• Similar to real but occupies twice the storage space as real. i.e. it occupies 8
bytes of memory.
• Can store values from 2.2 × 10-308 to 1.8 × 10308
▪ Complex:
• It can be used to store an ordered pair of data representing real and imaginary
part of a complex number.
• It occupies 8 bytes of memory.
▪ Logical:
• It can be used to store Boolean data representing TRUE (1) or FALSE (0).
• It occupies just 1 bit of memory.

FORTRAN NOTES 7
Data Types, Constants & Variables:
▪ Character:
• It can be used to store one alphanumeric character.
• It occupies one byte of memory.
▪ Constants:
• Its value does not change in the program.
• The type of constant depends upon the data type mentioned earlier.
• Thus constant might be integer constant, real constant, complex constants,
logical constants, and character constants.
• For example:
• Integer constants: 1, -500, +567 are some examples of integer constant

FORTRAN NOTES 8
Data Types, Constants & Variables:
• Real Constants: It represents floating point number either with decimal
point or an exponent.
• For example: 2.5, 54.67 are some real constants with decimal point and
• 1E3 is a real constant in exponential form.
• Here 1E3 means 1 × 103. In this context, 1 is called Mantissa and 3 is called
exponent.
• Character Constants: ‘A’, ‘Ram’, and ‘Fortran Programming Language’ are
some examples of character constants.

▪ Variables:
• It value might change or can be modified in the program.
• The concept of variable in FORTRAN is similar to that in C.
• In FORTRAN the variable can be declared in two possible ways.
FORTRAN NOTES 9
Data Types, Constants & Variables:
They are:
i. Implicit Declaration:
• It is possible in FORTRAN to use variable name without explicitly declaring.
• In such cases the compiler implicitly assumes the type on the basis of the
starting
• character of the variable name or identifier.
• If the variable name start with i, j, k ,l, m or n compiler assumes it to be an
integer variable.
• All other starting letter in a variable would force compiler to assume it as a
real variable.

FORTRAN NOTES 10
Data Types, Constants & Variables:
ii. Explicit Declaration:
• As an example, the explicit declaration of variable usually takes following
form:
integer list of variables
real list of variables
logical list of variables
character list of variables

FORTRAN NOTES 11
Operators and Library Functions
▪ Operators:
• Operators operate on operands (usually variable or constant) representing some
operation.
• Operator and operands are usually used to form an expression.
• Following are the different types of operators:

i. Arithmetic Operators:
• Arithmetic operations are carried out through the use of Arithmetic operators.
• Following table summarizes different arithmetic operators along with their
associativity, precedence, and other information:

FORTRAN NOTES 12
Operators and Library Functions

FORTRAN NOTES 13
Operators and Library Functions
ii. Relational Operators:
• These operators are used to compare two similar operands.
• They can be used with control structure to achieve decision making.
• The result of using relational operator is either one meaning true or zero
meaning false.
• Following table summarizes different relational operators in FORTRAN:

FORTRAN NOTES 14
Operators and Library Functions
iii. Logical Operators:
• These operators are used to perform logical operations.
• The most basic logical operations are NOT, AND, and OR.
• Following table summarizes the use of basic logical operations:

FORTRAN NOTES 15
Operators and Library Functions
iv. String Concatenation Operators:
• The operator represented by double forward slash // is known as string
concatenation operator.
• It is also known as character operator.
• It can be used to join two character strings.
v. Assignment Operators:
• A variable assignment has a form
variable_name = expression
• The value of expression is evaluated first and the resulting value is assigned to
the variable. For example:
• b=a+2
• First, 2 is added to the value of a.
• The resulting value of the expression is assigned to the variable b.
FORTRAN NOTES 16
Operators and Library Functions

FORTRAN NOTES 17
Structure of a FORTRAN Program
• Any FORTRAN program consists of a main program and other associated functions
or subroutines.
• The discussion of functions in FORTRAN is beyond the scope of this course.
• The structure of the main program of the FORTRAN takes following format:
Program Name
Declarations
Statements
End
• FORTRAN 77 is not a free-format language, but has a very strict set of rules for how
the source code be formatted.
• The most important rules are the column position rules.

FORTRAN NOTES 18
Structure of a FORTRAN Program
• They are:
Column 1: Usually blank, or a “c” or “*” for writing comment
Column 2-5: Blank or a statement label
Column 7-72: Statements
Column 73-80: Sequence Number, rarely used.

FORTRAN NOTES 19
Structure of a FORTRAN Program
• In the above program, the letter “c” at column 1 indicates that the following line is a
• comment.
• Similarly, the statement write(*,*)‘Hello World!’ is labeled as 100 and it is written
within the columns 2-5.
• The statement

write(*,*)‘This is from the console of FORTRAN Program.’,


‘Illustrating the concept of writing multiple line statements.’

occupies two physical lines. So a “+” sign at column 6 indicates it is a continuation to the
same statement.

FORTRAN NOTES 20
Examples
WAP in FORTRAN to display “Hello World!”

c WAP to display Hello World! in ForTran


program hello
write(*,*)'Hello World!’
end program hello

FORTRAN NOTES 21
Examples
WAP in FORTRAN to display sum of two integer numbers entered by the user

c WAP to display sum of two numbers entered by the user


program addition
integer a,b,sum
write(*,*)'Enter first number:’
read(*,*)a
write(*,*)'Enter second number:’
read(*,*)b
sum=a+b
write(*,*)'The sum is:',sum
end program addition

FORTRAN NOTES 22
Examples
WAP in FORTRAN to display the sum of two real numbers entered by the user

c WAP to display sum of two real numbers entered by the user


program real_addition
real a,b,sum
write(*,*)'Enter first number:’
read(*,*)a
write(*,*)'Enter second number:’
read(*,*)b
sum=a+b
write(*,*)'The sum is:',sum
end program real_addition

FORTRAN NOTES 23
Examples
WAP in FORTRAN to display the sum of two complex numbers entered by the user

c WAP to display sum of two complex numbers entered by the user


program complex_addition
complex a,b,sum
write(*,*)'Enter first complex number:’
read(*,*)a
write(*,*)'Enter second complex number:’
read(*,*)b
sum=a+b
write(*,*)'The sum is:',sum
end program complex_addition

FORTRAN NOTES 24
Examples
WAP in FORTRAN to display a character entered by the user

C WAP in FORTRAN to display a character entered by the user


program display_character
character ch
write(*,*)'Enter a character’
read(*,*)ch
write(*,*)'Entered character is',ch
end program display_character

FORTRAN NOTES 25
Examples
WAP in FORTRAN to display a string entered by the user

c WAP in FORTRAN to display a string entered by the user


program display_string
character*10 str
write(*,*)'Enter a string’
read(*,*)str
write(*,*)'Entered string is',str
end program display_string

FORTRAN NOTES 26
Examples
WAP in FORTRAN to concatenate two strings entered by the user and display it

c WAP in FORTRAN to concatenate two strings entered by the user and


c display it
program concatenate_string
character*10 str1,str2
write(*,*)'Enter first string’
read(*,*)str1
write(*,*)'Enter second string’
read(*,*)str2
write(*,*)'concatenated string is',str1//str2
end program concatenate_string

FORTRAN NOTES 27
Examples
WAP in FORTRAN to calculate simple interest from data supplied by the user

program simple_interest
real principal, time, rate, interest
write(*,*)'Enter the value’,
+ ' of principal, time, and rate’
read(*,*)principal,time,rate
interest = (principal*time*rate)/100
write(*,*)'The simple interest earned is Rs.',interest
end program simple_interest

• Note the use of “+” in column 6 to represent following two lines


write(*,*)'Enter the value',
+' of principal, time, and rate’
as a single statement.
FORTRAN NOTES 28
Formatted & Unformatted IO Functions
• When the input and output operations are carried out in some specific pattern
under the control of some format specification then it is known as formatted IO
while if it is carried out without those, it is known as unformatted IO.
• In FORTRAN, read() and write() functions can be used in conjunction with a format
specifier to implement formatted IO.
• Until now, we used read() and write() to implement unformatted IO.
• The general syntax of read() and write() are as follows:
read(unit#,format#)list of arguments
write(unit#,format#)list of arguments

FORTRAN NOTES 29
Formatted & Unformatted IO Functions
• The unit is a number which has an association with a particular device.
• The device can be Terminal or a FILE.
• The unit is an integer or an integer expression with the value in the range 1-30.
• Standard FORTRAN reserves two unit numbers to provide IO to user. Unit = 5 is
reserved for input from the keyboard using read statement and unit = 6 is reserved
for output to the screen using write statement.
• When * is used in place of unit# and format#, the default input/output device is
assumed and default format or free format is assumed.
• The syntax for specifying format is as follows:
label FORMAT(format_specifications)

FORTRAN NOTES 30
Formatted & Unformatted IO Functions
• The label in the above syntax is a number which when included in the format
number of read/write enforces the format specifications during IO operation.
• The format code letters to specify format specifications are as follows:
i. I Format:
• Used for formatting integer constant or variable.
• Syntax: Iw, where w is the width of the integer data.
ii. F Format:
• Syntax: Fw.d, where w is the total width and d is precision of real data.
Iii. E Format:
• Used for formatting real number in exponential form
• Syntax: E w.d, where w is the total field width including mantissa and d is the decimal
width or precision of the mantissa

FORTRAN NOTES 31
Formatted & Unformatted IO Functions
iv. X Format:
• Used for skipping columns in displaying the data
• Syntax: nX, skips n columns
v. A Format:
• Syntax: Aw, where w is the width of character data type A.
vi. T Format:
• Used for starting output from nth column
• Syntax: nT, where n is the column number from which output starts.

Carefully examine given example programs in next slides to gain some insights on
formatted IO in FORTRAN:

FORTRAN NOTES 32
Examples
WAP in ForTran to illustrate the concept of formatted IO

c WAP to illustrate the concept of formatted input and output


program formatted_io_concept
integer f,s,t
write(6,*)'Enter the value of f,s,t:’
read(5,100)f,s,t
100 format(I3,I4,I2)
write(6,*)'Data according to format 100:’
write(6,100)f,s,t
write(6,*)'Data according to format 200:’
write(6,200)f,s,t
200 format(I3,2x,I4,2x,I2)
end program formatted_io_concept

FORTRAN NOTES 33
Examples
WAP in FORTRAN to add two complex numbers entered by the user by using the
concept of FORMATTED IO without using complex type
program complex_sum
real x1,y1,x2,y2,x3,y3
character*2 ch
write(*,*)'Enter first complex number’
read(*,100)x1,ch,y1
write(*,*)'Enter second complex number’
read(*,100)x2,ch,y2
x3 = x1 + x2
y3 = y1 + y2
write(*,200),x3,x3
100 FORMAT(F4.2,A2,F4.2)
200 FORMAT('The sum is',F4.2,'+i',F4.2)
end program complex_sum

FORTRAN NOTES 34
Control Structures
GOTO, Logical IF, Arithmetic IF, DO LOOPS
• A control structure is a block of programming that analyzes variables and chooses a
direction in which to go based on given parameters.
• In FORTRAN, various control structures are available, some of which are discussed
here:

FORTRAN NOTES 35
Control Structures
GOTO:
• Goto statement is used to jump from instruction at one location to other.
• Goto can be implemented as unconditional and computed.
i. Unconditional Goto
Syntax: goto label
• The above statement causes the program to execute the instruction/statement at label
mentioned.
ii. Computed Goto
• In computed goto, the value of an integer expression is evaluated, and the control is
transferred to the statement number/index matching its value.
• Syntax:
goto (s1,s2,s3,….sk) integer expression

FORTRAN NOTES 36
Control Structures
GOTO
• When value of integer expression is equal to 1, control is transferred to statement
number s1.
• If it is equal to 2, control is transferred to statement number s2.
• Finally if it is equal to k, control is transferred to statement number sk.
• If the value of integer expression is negative or greater than k, the goto statement is
ignored.

FORTRAN NOTES 37
Examples
WAP in FORTRAN to determine whether a number entered by the user is prime or composite using the concept of goto
c WAP in FORTRAN to determine whether a number entered
c by the user is prime or composite program prime_or_composite
integer n,i
write(*,*)'Enter a number’
read(*,*)n
do 200, i=2,n-1,1
if(mod(n,i).eq.0) then
write(*,*)'Composite number!’
goto 201
end if
200 continue
write(*,*)'Prime number!’
201 stop
end program prime_or_composite
FORTRAN NOTES 38
Examples
WAP in FORTRAN to find square or cube of entered number using the concept of computed goto
program computed_goto
integer n,i
write(*,*)'Enter an integer’
read(*,*)n
write(*,*)'Enter 1 to find square’
write(*,*)'Enter 2 to find cube’
read(*,*)i
goto(100,200)i
write(*,*)'Enter the correct choice!’
goto 300
100 write(*,*)'The square of entered number is',n*n
goto 300
200 write(*,*)'The cube of entered number is',n*n*n
300 end program computed_goto
FORTRAN NOTES 39
Examples
WAP in FORTRAN to display the multiplication table of a number entered by the user using the concept of
unconditional goto
program multiplication_table
integer n,i
i=1
write(*,*)'Enter a number’
read(*,*)n
200 write(*,201)n,'X',i,'=',n*i
201 format(I1,A1,I2,A1,I3)
i=i+1
if(i.le.10) then
goto 200
end if
end program multiplication_table

FORTRAN NOTES 40
Control Structures
Logical IF
• Logical IF is a simple branching statement which helps to select one possible path
out of several possible paths depending upon the value of variable or decision
criteria.
• In FORTRAN, LOGICAL IF has three forms, they are Simple IF…, IF….ELSE, IF…ELSE
IF…ELSE.

• Simple Logical IF:


Syntax:
IF (expression) THEN
Statement(s)
ENDIF
Statement X
Statements(s) are executed only if the expression is true. Afterwards, statement X is executed.
FORTRAN NOTES 41
Control Structures
Logical IF

IF….ELSE
Syntax:
IF (expression) THEN
Statement1
ELSE
Statement 2
ENDIF
Statement1 is executed if the expression is true otherwise, statement2 is executed.

FORTRAN NOTES 42
Control Structures
Logical IF
IF…ELSE IF…ELSE
Syntax:
IF(expression1) THEN
Statement1
ELSE IF(expression2) THEN
Statement2
….
ELSE IF(expressionn) THEN
Statementn
ELSE
Defaults statement
ENDIF
IF expression 1 is true, then statement1 would be executed. Likewise, if expression2 is true then
statement2 is executed and so on. If none of the expressions are true, default statement is executed.

FORTRAN NOTES 43
Examples
WAP in FORTRAN to determine whether a number entered by the user is even or odd using the concept
of logical IF

program even_or_odd
integer n
write(*,*)'Enter a number’
read(*,*)n
if(mod(n,2).eq.0) then
write(*,*)'Entered number is even’
else
write(*,*)'Entered number is odd’
end if
end program even_or_odd

FORTRAN NOTES 44
Examples
WAP to determine greatest number among three numbers entered by the user using the concept of IF… ELSE
IF…ELSE
c WAP to determine greatest number among three numbers entered by
c the user.
program greatest_among_three
integer a,b,c
write(*,*)'Enter first number:’
read(*,*)a
write(*,*)'Enter second number:’
read(*,*)b
write(*,*)'Enter third number:’
read(*,*)c
If(a.gt.b.and.a.gt.c) then
write(*,*)'The greatest number is:’,a
else if(b.gt.a.and.b.gt.c) then
FORTRAN NOTES 45
Examples
write(*,*)'The greatest number is:’,b
else
write(*,*)'The greatest number is:’,c
endif
end program greatest_among_three

FORTRAN NOTES 46
Examples
WAP in FORTRAN to display all the odd numbers from 1 to 100

program display_odd
write(*,*)'Displaying odd numbers from 1 to 100’
do 100, i=1,100,1
if(mod(i,2).NE.0) then
write(*,*)i
endif
100 continue
end program display_odd

FORTRAN NOTES 47
Control Structures
Arithmetic IF:
• Arithmetic IF is used to transfer control of the program to a particular statement
depending upon whether the value of an expression is negative, zero or positive.
• The syntax of arithmetic if is:
IF(expression) s1,s2,s3
• First the value of expression is evaluated.
• The program branches to the statement number s1 if the value is negative, to the
statement number s2 if the value is zero and to the statement number s3 if the
value is positive.

FORTRAN NOTES 48
Examples
WAP in FORTRAN to determine whether a number entered by the user is negative, zero or positive
using the concept of Arithmetic IF
program positive_zero_or_negative
integer n
write(*,*)'Enter a number’
read(*,*)n
if(n) 200,201,202
200 write(*,*)'Entered number is negative’
goto 203
201 write(*,*)'Entered number is zero’
goto 203
202 write(*,*)'Entered number is positive’
203 stop
end program positive_zero_or_negative

FORTRAN NOTES 49
Control Structures
DO LOOP:
• The process of repeating a block of statements until some condition is satisfied is
known as looping.
• Though a loop can be realized using the goto statement, FORTRAN has a provision
of DO LOOP.
• The syntax of DO LOOP is:
do label, integer_variable = initial_value, final_value, step_size
Block of statement(s)
label continue
The block of statements are executed until the value of the variable is less than or equal to
final value.
This loop is similar to for loop in C programming language.
The alternative syntax for do…loop without using the label and continue statements is:
do integer_variable = initial_value,final_value,step_size
Block of statements(s)
enddo FORTRAN NOTES 50
Examples
WAP in FORTRAN to display the multiplication table of a number entered by the user using the concept of DO
LOOP
program multiplication_table
integer n,i
write(*,*)'Enter a number’
read(*,*)n
do 200,i=1,10,1
write(*,201)n,'X',i,'=',n*i
200 continue
201 format(I1,A1,I2,A1,I3)
end program multiplication_table

FORTRAN NOTES 51
Examples
Alternatively the same program can be implemented as:

program multiplication_table
integer n,i
write(*,*)'Enter a number’
read(*,*)n
do i=1,10,1
write(*,200)n,'X',i,'=',n*i
enddo
200 format(I1,A1,I2,A1,I3)
end program multiplication_table

FORTRAN NOTES 52
Arrays: One Dimensional & Two Dimensional Array
• An array is a group of variables that share a common name and reside in contiguous
memory locations.
• The concept of array in FORTRAN is similar to that in C with two major exceptions:
first, the index of array must start from 1 and end at size of the array, and second,
the indexing operator is ( ) instead of [ ].
• The syntax for declaring one dimensional array is:
data_type array_name(size)
Example: integer a(50)
• The above statement declares one dimensional array a with size 50.
• In other words, fifty integers a(1), a(2),….a(50) have been declared.
• The syntax for declaring two dimensional arrays is:
data_type array_name(row_size,column_size)
Example: integer a(3,3)
FORTRAN NOTES 53
Arrays: One Dimensional & Two Dimensional Array
The above statement declares a two dimensional array a as follows:
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)

FORTRAN NOTES 54
Arrays: One Dimensional & Two Dimensional Array
Initializing array elements:
• Array elements can be initialized in two ways. They are:
i. By using Data Statement
• The syntax of data statement is:
data variable list/value list/
• For example:
data a, b, c/1,2,3/
• Above statement assigns 1 to a, 2 to b and 3 to c
• Similarly,
integer a(5)
data a(1), a(2), a(3), a(4), a(5)/4,6,8,2,4/
The above statement assigns 4 to a(1), 6 to a(2), 8 to a(3), 2 to a(4) and 4 to a(5)
FORTRAN NOTES 55
Arrays: One Dimensional & Two Dimensional Array
ii. By using Implied do loop
• The syntax of implied do loop is:
array_name(counter_variable),counter_variable=initial_value,final_value,step_size

• Example:
integer a(50)
read(*,*)a(i),i=1,50,1
• using the implied do loop: a(i),i=1,50,1, read(*,*) can read values into entire array at
once.
• Without using the implied do loop, it could be done as:
do 100, i =1,50,1
read(*,*)a(i)
100 continue
• Note the relative compactness achieved with the use of implied do loop.
FORTRAN NOTES 56
Examples
WAP in FORTRAN to create an integer array a of size 5 and initialize it to the values {2, 7, 13, 89, 50} using the
concept of data statement.

program initializing_array_data
integer a(5)
data a(1),a(2),a(3),a(4),a(5)/2, 7, 13, 89, 50/
do 100, i=1,5,1
write(*,*)a(i)
100 continue
end program initializing_array_data

FORTRAN NOTES 57
Examples
Repeat the same problem using implied do loop

program initializing_array_data
integer a(5)
data(a(i),i=1,5,1)/2, 7, 13, 89, 50/
do 100, i=1,5,1
write(*,*)a(i)
100 continue
end program initializing_array_data

FORTRAN NOTES 58
Examples
WAP in FORTRAN to find the greatest number among n numbers entered by the user

c WAP in FORTRAN to find the greatest number among n numbers entered by


c the user
program greatest_among_n
integer a(50),n,g,i
write(*,*)'Enter number of terms:’
read(*,*)n
write(*,*)'Enter the numbers:’
read(*,*)(a(i),i=1,n,1)
g=a(1)
do 100,i=1,n,1
if(a(i).gt.g) then
g=a(i)
endif
FORTRAN NOTES 59
Examples
100 continue
write(*,*)'The greatest number is:’,g
end program greatest_among_n

FORTRAN NOTES 60
Examples
WAP in FORTRAN to find the least number among n numbers entered by the user
c WAP to find the least number among n numbers entered by the user
c program least_among_n
integer a(50),n,l,i
write(*,*)'Enter number of terms:’
read(*,*)n
write(*,*)'Enter the numbers:’
read(*,*)(a(i),i=1,n,1)
l=a(1)
do 100,i=1,n,1
if(a(i).le.l) then
l=a(i)
endif
100 continue
write(*,*)'The least number is:’,l
end program least_among_n FORTRAN NOTES 61
Examples
WAP in FORTRAN to sort n numbers entered by the user in ascending order.

c WAP to sort n numbers entered by the user in Ascending order.


program sort_ascending
integer a(10),i,j,n
write(*,*)'Enter number of terms:’
read(*,*)n
write(*,*)'Enter the numbers to be sorted.’
read(*,*)(a(i),i=1,n,1)
do 100, i=1,n,1
do 200, j=i+1,n,1
if(a(i).ge.a(j))then
temp=a(j)
a(j)=a(i)
a(i)=temp
FORTRAN NOTES 62
Examples
endif
200 continue
100 continue
write(*,*)'The entered numbers in ascending order is:’
write (*,*)(a(i),i=1,n)
end program sort_ascending

FORTRAN NOTES 63
Examples
WAP in FORTRAN to sort n numbers entered by the user in descending order.
c WAP to sort n numbers entered by the user in descending order.
program descending_sort
integer a(20),i,j,k,l
temp=0
write(*,*)'Enter number of terms:’
read(*,*)n
write(*,*)'Enter the numbers to be sorted.’
read(*,*)(a(l),l=1,n,1)
do 100,i=1,n,1
do 200,j=i+1,n,1
if(a(i).lt.a(j))then
temp=a(i)
a(i)=a(j)
a(j)=temp
FORTRAN NOTES 64
endif
Examples
200 continue
100 continue
write(*,*)'The entered numbers in descending order is:’
do 300,k=1,n,1
write(*,*)a(k)
300 continue
end program descending_sort

FORTRAN NOTES 65
Examples
WAP in ForTran to read and display a 3x3 matrix

c WAP in ForTran to read and display a 3x3 matrix


c program read_and_display_matrix
integer a(3,3)
write(*,*)'Enter the matrix’
do 100, i=1,3,1
read(*,*)(a(i,j),j=1,3,1)
100 continue
write(*,*)'The entered matrix is:’
do 200, i=1,3,1
write(*,*)(a(i,j),j=1,3,1)
200 continue
end program read_and_display_matrix

FORTRAN NOTES 66
Examples
WAP in ForTran to find transpose of a 3x3 matrix entered by the user.
c WAP in fortran to find a transpose of a 3x3 matrix entered by the
c user.
program transpose
integer a(3,3),c(3,3)
write(*,*)'Enter the matrix’
do 100, i=1,3,1
do 200, j=1,3,1
read(*,*)a(i,j)
c(j,i)=a(i,j)
200 continue
100 continue
write(*,*)'The entered matrix is:’
do 300, i=1,3,1
write(*,*)(a(i,j),j=1,3,1)
FORTRAN NOTES 67
Examples
300 continue
write(*,*)'The transpose of entered matrix is:’
do 400, i=1,3,1
write(*,*)(c(i,j),j=1,3,1)
400 continue
end program transpose

FORTRAN NOTES 68
Examples
WAP to find sum of two 3x3 matrices entered by the user.

c WAP to find sum of two 3x3 matrices entered by the user.


c program matrix_sum
integer a(3,3),b(3,3),c(3,3),sum
write(*,*)'Enter first matrix’
do 100, i=1,3,1
read(*,*)(a(i,j),j=1,3,1)
100 continue
write(*,*)'Enter second matrix’
do 200, i=1,3,1
read(*,*)(b(i,j),j=1,3,1)
200 continue
do 300, i=1,3,1
do 400, j=1,3,1
FORTRAN NOTES 69
c(i,j)=a(i,j)+b(i,j)
Examples
400 continue
300 continue
write(*,*)'The sum matrix is’
do 600, i=1,3,1
write(*,*)(c(i,j),j=1,3,1)
600 continue
end program matrix_sum

FORTRAN NOTES 70
Examples
WAP to find product of two 3x3 matrices entered by the user.

c WAP to find product of two 3x3 matrices entered by the user.


c program matrix_product
integer a(3,3),b(3,3),c(3,3),sum,i,j,k
write(*,*)'Enter first matrix’
do 100, i=1,3,1
read(*,*)(a(i,j),j=1,3,1)
100 continue
write(*,*)'Enter second matrix’
do 200, i=1,3,1
read(*,*)(b(i,j),j=1,3,1)
200 continue
do 300, i=1,3,1
do 400, j=1,3,1
FORTRAN NOTES 71
sum=0
Examples
do 500, k=1,3,1
sum=sum+a(i,k)*b(k,j)
500 continue
c(i,j)=sum
400 continue
300 continue
write(*,*)'The product matrix is’
do 600, i=1,3,1
write(*,*)(c(i,j),j=1,3,1)
600 continue
end program matrix_product

FORTRAN NOTES 72
Any Queries???
Thankyou !!!

FORTRAN NOTES 73

You might also like