You are on page 1of 20

Chapter 11

PROGRAMMING LANGUAGE: FORTRAN


Course Outline:- -8 Hours
11.1 CHARACTER SET
11.2 DATA TYPES, CONSTANTS AND VARIABLES
11.3 ARITHMETIC OPERATIONS, LIBRARY FUNCTIONS
11.4 STRUCTURE OF A FORTRAN PROGRAM
11.5 FORMATTED AND UNFORMATTED IO OPERATIONS
11.6 CONTROL STRUCTURES: GOTO, LOGICAL IF, ARITHMETIC IF, DO LOOPS
11.7 ARRAYS: ONE DIMENSIONAL AND TWO DIMENSIONAL
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. 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:
S.N. Version Released Date
1. FORTRAN 1957 April, 1957
2. FORTRAN II 1958
3. FORTRAN III 1958
3. FORTRAN IV 1962
4. FORTRAN 66 March, 1966
5. FORTRAN 77 1977
6. FORTRAN 90 1991
7. FORTRAN 95 1995
As far as this course is concerned, we shall study FORTRAN with reference to the standards
of FORTRAN 77.
CHARACTER SET:

Following are the different character set supported by FORTRAN:


Letters: 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(”)

Computer Programming [CT 451] Page Number: 1 Prepared by: Bikal Adhikari
DATA TYPES, CONSTANTS AND VARIABLES:

Data Types:
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
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.
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.
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. 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.

ii. Explicit Declaration: As an example, the explicit declaration of variable usually takes
following form:
integer list of variables

Computer Programming [CT 451] Page Number: 2 Prepared by: Bikal Adhikari
real list of variables
logical list of variables
character list of variables

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:
Associativity Precedence Operation Symbol FORTRAN Arithmetic
Expression
Right to left Highest Exponentiation ** X**Y XY
Left to right High Multiplication and * X*Y X×Y
division / X/Y X/Y
Left to right Low Addition, + X+Y X+Y
Subtraction - X-Y X–Y
and -X -X
unary minus
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:
Relational Equivalent Operation FORTRAN Result assuming
Operator C operator Expression x = 8 and y = 5
.LT. < Less than x.LT.y 0
.GT. > Greater than x.GT.y 1
.LE. <= Less than or equal to x.LE.y 0
.GE. >= Greater than or equal to x.GE.y 1
.EQ. == Equal to x.EQ.y 0
.NE. != Not equal to x.NE.y 1
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:
X Y .NOT.X X.AND.Y X.OR.Y
.FALSE. .FALSE. .TRUE. .FALSE. .FALSE.
.FALSE. .TRUE. .TRUE. .FALSE. .TRUE.
.TRUE. .FALSE. .FALSE. .FALSE. .TRUE.
.TRUE. .TRUE. .FALSE. .TRUE. .TRUE.

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

Computer Programming [CT 451] Page Number: 3 Prepared by: Bikal Adhikari
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.
SOME LIBRARY FUNCTIONS AVAILABLE IN FORTRAN
abs(x) asin(x)
exp(x) acos(x)
sqrt(x) atan(x)
log(x) sinh(x)
sin(x) cosh(x)
cos(x) tanh(x)
tan(x) etc…
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. 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.
For Example:-
1 2-5 6 7-72 73-80
c WAP to display Hello World! in FORTRAN
program hello
100 write(*,*) „Hello World!‟
write(*,*)„This is from the console of FORTRAN Program.‟,
+ „Illustrating the concept of writing multiple line statements.‟
end program hello

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

Computer Programming [CT 451] Page Number: 4 Prepared by: Bikal Adhikari
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.
EXAMPLE PROGRAMS TO REINFORCE CONCEPTS ACQUIRED

WAP in FORTRAN to display “Hello World!”


c WAP to display Hello World! in ForTran
program hello
write(*,*)'Hello World!'
end program hello

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

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

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

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

Computer Programming [CT 451] Page Number: 5 Prepared by: Bikal Adhikari
write(*,*)'Enter a character'
read(*,*)ch
write(*,*)'Entered character is',ch
end program display_character

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

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

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.
FORMATTED AND UNFORMATTED IO OPERATIONS

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

Computer Programming [CT 451] Page Number: 6 Prepared by: Bikal Adhikari
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)

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
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 following example programs to gain some insights on formatted IO in
FORTRAN:
WAP in ForTran to illustrate the concept of formatted IO
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

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

Computer Programming [CT 451] Page Number: 7 Prepared by: Bikal Adhikari
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

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.
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
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.
WAP in FORTRAN to determine whether a number entered by the user is prime or
composite using the concept of goto
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
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

Computer Programming [CT 451] Page Number: 8 Prepared by: Bikal Adhikari
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
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

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.
IF….ELSE
Syntax:
IF (expression) THEN
Statement1
ELSE
Statement 2
ENDIF
Statement1 is executed if the expression is true otherwise, statement2 is executed.
IF…ELSE IF.…ELSE
Syntax:
IF(expression1) THEN
Statement1
ELSE IF(expression2) THEN

Computer Programming [CT 451] Page Number: 9 Prepared by: Bikal Adhikari
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.
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

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
write(*,*)'The greatest number is:',b
else
write(*,*)'The greatest number is:',c
endif
end program greatest_among_three
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

Computer Programming [CT 451] Page Number: 10 Prepared by: Bikal Adhikari
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.
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

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
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

Computer Programming [CT 451] Page Number: 11 Prepared by: Bikal Adhikari
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

ARRAYS: ONE DIMENSIONAL AND TWO DIMENSIONAL

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)

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)

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)

Computer Programming [CT 451] Page Number: 12 Prepared by: Bikal Adhikari
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: Observe the relative compactness achieved with the use of implied do loop.
FEW EXAMPLE PROGRAMS

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

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

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
100 continue

Computer Programming [CT 451] Page Number: 13 Prepared by: Bikal Adhikari
write(*,*)'The greatest number is:',g
end program greatest_among_n

WAP in FORTRAN to find the least number among n numbers entered by the user
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

WAP in FORTRAN 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
endif
200 continue
100 continue
write(*,*)'The entered numbers in ascending order is:'
write (*,*)(a(i),i=1,n)
end program sort_ascending

WAP in FORTRAN 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
endif
200 continue
100 continue
write(*,*)'The entered numbers in descending order is:'
do 300,k=1,n,1
write(*,*)a(k)
300 continue

Computer Programming [CT 451] Page Number: 14 Prepared by: Bikal Adhikari
end program descending_sort

WAP in ForTran to read and display a 3x3 matrix


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

WAP in ForTran to find transpose of a 3x3 matrix entered by the 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)
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
WAP to find sum of two 3×3 matrices entered by the user.
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
c(i,j)=a(i,j)+b(i,j)
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

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


program matrix_product
integer a(3,3),b(3,3),c(3,3),sum,i,j,k

Computer Programming [CT 451] Page Number: 15 Prepared by: Bikal Adhikari
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
sum=0
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

ADDITIONAL PROGRAMMING EXAMPLES IN FORTRAN

WAP in FORTRAN program to read n numbers and display the largest among them.
– 2073 Shrawan
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 i=1,n,1
if(a(i).gt.g) then
g=a(i)
endif
enddo
write(*,*)'The greatest number is:',g
end program greatest_among_n

Write a FORTRAN program to add and subtract two matrices and display the result in
matrix form -2072 Chaitra
program matrix_sum_diff
integer a(3,3),b(3,3),c(3,3),d(3,3),sum
write(*,*)'Enter first matrix'
do i=1,3,1
read(*,*)(a(i,j),j=1,3,1)
enddo
write(*,*)'Enter second matrix'
do i=1,3,1
read(*,*)(b(i,j),j=1,3,1)
enddo
do i=1,3,1
do j=1,3,1
c(i,j)=a(i,j)+b(i,j)
d(i,j)=a(i,j)-b(i,j)
enddo
enddo

Computer Programming [CT 451] Page Number: 16 Prepared by: Bikal Adhikari
write(*,*)'The sum matrix is'
do i=1,3,1
write(*,*)(c(i,j),j=1,3,1)
enddo
write(*,*)'The difference matrix is'
do i=1,3,1
write(*,*)(d(i,j),j=1,3,1)
enddo
end program matrix_sum_diff

Write the program to convert a binary number to decimal number using FORTRAN
programming language -2071 Shrawan
program binary_to_decimal
integer n,i,r,s
i=1
r=0
s=0
write(*,*)'Enter a binary number'
read(*,*)n
100 r=mod(n,10)
s=s+r*i
i=i*2
n=n/10
if(n.gt.0) then
goto 100
endif
write(*,*)'Decimal equivalent =',s
stop
end program binary_to_decimal

WAP in FORTRAN, to check whether a positive integer entered from the keyboard is
palindrome or not. -2068 Chaitra
program palindrome_check
integer n,nc,r,s
s=0
write(*,*)'Enter a positive integer'
read(*,*)n
nc=n
100 r=mod(n,10)
s=s*10+r
n=n/10
if(n.gt.0) then
goto 100
endif
if(s.eq.nc) then
write(*,*)'Entered number is palindrome'
else
write(*,*)'Entered number is not palindrome'
endif
stop
end program palindrome_check

Write a FORTRAN program to display greatest and smallest number from a list of ten
elements. -2070 Chaitra
program find_greatest_and_smallest
integer a(10),i,j
write(*,*)'Enter ten numbers.'
read(*,*)(a(i),i=1,10,1)
do i=1,10,1
do j=i+1,10,1
if(a(i).ge.a(j))then

Computer Programming [CT 451] Page Number: 17 Prepared by: Bikal Adhikari
temp=a(j)
a(j)=a(i)
a(i)=temp
endif
enddo
enddo
write(*,*)'The greatest number is',a(10)
write(*,*)'The smallest number is',a(1)
end program find_greatest_and_smallest

Write a FORTRAN program to read m*n matrix, transpose it and display both the
matrices. -2070 Chaitra
program transpose_matrix
integer a(10,10),c(10,10),m,n,i,j
write(*,*)'Enter order m by n of the matrix'
read(*,*)m,n
write(*,*)'Enter the matrix'
do i=1,m,1
do j=1,n,1
read(*,*)a(i,j)
c(j,i)=a(i,j)
enddo
enddo
write(*,*)'The entered matrix is:'
do 300, i=1,m,1
write(*,*)(a(i,j),j=1,n,1)
300 continue
write(*,*)'The transpose of entered matrix is:'
do 400, i=1,n,1
write(*,*)(c(i,j),j=1,m,1)
400 continue
end program transpose_matrix

Write a program to read a day number and display whether it is Sunday, Monday,
Tuesday, Wednesday, Thursday, Friday and Saturday using computed goto
-2069 Chaitra
program find_day
integer day
write(*,*)'Enter the day number'
read(*,*)day
goto (100,200,300,400,500,600,700)day
write(*,*)'Enter a valid day number. Exiting...'
goto 800
100 write(*,*)'The entered day is Sunday'
goto 800
200 write(*,*)'The entered day is Monday'
goto 800
300 write(*,*)'The entered day is Tuesday'
goto 800
400 write(*,*)'The entered day is Wednesday'
goto 800
500 write(*,*)'The entered day is Thursday'
goto 800
600 write(*,*)'The entered day is Friday'
goto 800
700 write(*,*)'The entered day is Saturday'
800 stop
end program find_day

Computer Programming [CT 451] Page Number: 18 Prepared by: Bikal Adhikari
Write a program in FORTRAN to evaluate the following series. -2067 Ashwin
1 1 1 1
series = 12 + 22 + 32 + … + n2

program series_sum
real s
integer n,i
s=0
write(*,*)'Enter number of terms'
read(*,*)n
do i = 1,n,1
s = s + 1.00/(i**2)
enddo
write(*,*)'Sum of series = ', s
end program series_sum

Write a program in FORTRAN to calculate the value of π by evaluating the following


formula for 25 terms. -2074 Ashwin
 1 1 1 1 
π = 41 - 3 + 5 - 7 + 9 - …
 
program series_sum_pi
real s
integer i
s=0
do i = 1,25,1
s = s + 1.00*((-1)**(i+1))/(2*i-1)
enddo
s = 4*s
write(*,*)'value of pi = ', s
end program series_sum_pi

Write a program to read n from user and display the sum of following series till nth
terms: 1 + (1+2) + (1+2+3) + (1+2+3+4) +…..upto nth term -2072 Kartik
program find_sum
integer n,sump,sumf
sumf=0
write(*,*)'Enter the number of terms'
read(*,*)n
do i=1,n,1
sump=0
do j=1,i,1
sump=sump+j
enddo
sumf=sumf+sump
enddo
write(*,*)'The sum is:',sumf
end program find_sum

Write a program in FORTRAN to solve a quadratic equation and display the roots in
proper format. -2070 Ashad
program solve_quadratic_eqn
real a,b,c,d,r1,r2,real,imag
write(*,*)'Enter the coefficients a,b and c:'
read(*,*)a,b,c
d = b**2 - 4*a*c
if(d.eq.0) then
r1 = -b/(2*a)
r2 = r1
write(*,*)'The roots of given equation are real and equal'

Computer Programming [CT 451] Page Number: 19 Prepared by: Bikal Adhikari
write(*,*)'r1=r2=',r1
else if(d.gt.0) then
r1 = (-b-sqrt(d))/(2*a)
r2 = (-b+sqrt(d))/(2*a)
write(*,*)'The roots of given equation are real and unequal'
write(*,*)'r1=',r1,'r2=',r2
else
real = -b/(2*a)
imag = (sqrt(abs(d)))/(2*a)
write(*,*)'The roots are imaginary and complex conjugates'
write(*,*)'r1=',real,'+',imag,'i'
write(*,*)'r2=',real,'-',imag,'i'
endif
end program solve_quadratic_eqn
WAP in ForTran to display the following pattern
1
12
123
1234
12345
program pattern
integer i,j
do 100, i = 1,5,1
write(*,*)(j,j=1,i)
100 continue
end program pattern

WAP in ForTran to display following pattern


54321
4321
321
21
1
program pattern
integer i,j
do 100, i = 5,1,-1
write(*,*)(j,j=i,1,-1)
100 continue
end program pattern

Computer Programming [CT 451] Page Number: 20 Prepared by: Bikal Adhikari

You might also like