Fortran Note
Fortran Note
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:
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
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 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
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 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
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
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:
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
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)
Computer Programming [CT 451] Page Number: 12 Prepared by: Bikal Adhikari
ii. By using Implied do loop
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
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
Computer Programming [CT 451] Page Number: 14 Prepared by: Bikal Adhikari
end program descending_sort
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
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 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
Computer Programming [CT 451] Page Number: 20 Prepared by: Bikal Adhikari