All the examples can be executed in a Linux machine using the Gfortran compiler.
The console appears but we cannot see the result
- Configure the IDE to (not) minimize after running
- Create an executable file and run in console
- Create a bat file and run in console
- Include a final read sentence
Program Hello Print *, "Hello World!" read *, End Program Hello
Program variables
integer i1,i2
read *,i1,i2
print *,i1,i2
read *,
end program variables
"Create a program and declare variables of different datatypes"
Program simpleDatatypes
integer :: i1 = 0
real :: r1 = 1.0
character :: char1 = 'c'
complex :: c1
logical :: l1 = .true.
c1 = CMPLX(2, 4)
print *,i1, r1, char1, c1, logical
read *,
end program simpleDatatypes
Program arithmeticOperators
print *, (5+2)
print *, (5-2)
print *, (5*2)
print *, (5/2)
print *, (5**2)
read *,
end Program arithmeticOperators
MOD(A,P)
computes the remainder of the division of A by P.
Program logicalOperators
print *, (.NOT. .TRUE.)
print *, (.TRUE. .AND. .TRUE.)
print *, (.TRUE. .OR. .FALSE.)
print *, (1 < 2)
print *, (1 > 2)
print *, (1 < 2)
print *, (1 >= 2)
print *, (1 <= 2)
read *,
end Program logicalOperators
"Create a program and combine all type of operator: arithmetic, logical and relational. Assign values to different variables"
- If a number is greater than 5 then print "It is greater than 5"
Program basicIF
integer :: top = 5
integer :: number = 0
read *, number
IF (number > top) then
print *, "It is greater than ", top
END IF
read *,
end Program basicIF
- If a number is greater than 5 then print "It is greater than 5" otherwise print "It is less than 5"
Program basicIFElse
integer :: top = 5
integer :: number = 0
read *, number
IF (number > top) then
print *, "It is greater than ", top
ELSE
print *, "It is less than ", top
END IF
read *,
end Program basicIFElse
IF (logical-expression-1) THEN
statements-1
ELSE IF (logical-expression-2) THEN
statements-2
ELSE IF (logical-expression-3) THEN
statement-3
ELSE IF (.....) THEN
...........
ELSE
statements-ELSE
END IF
IF (a < b .AND. a < c) THEN
Result = a
ELSE IF (b < a .AND. b < c) THEN
Result = b
ELSE
Result = c
END IF
SELECT CASE (selector) CASE (label-list-1) statements-1 CASE (label-list-2) statements-2 CASE (label-list-3) statements-3 ............. CASE (label-list-n) statements-n CASE DEFAULT statements-DEFAULT END SELECT
INTEGER :: year SELECT CASE (year) CASE (1) WRITE(*,*) 'Freshman' CASE (2) WRITE(*,*) 'Sophomore' CASE (3) WRITE(*,*) 'Junior' CASE (4) WRITE(*,*) 'Senior' CASE DEFAULT WRITE(*,*) "Unknown" END SELECT
- Write a program to show the first 20 natural numbers (descending and using different loops).
program desc20
integer :: a
do a = 20, 0, -1
print*,a
end do
a = 20
do while (a >= 0)
print*,a
a = a - 1
end do
end program desc20
DO
statements-1
IF (logical-expression) EXIT
statements-2
END DO
DO
statements-1
IF (logical-expression) THEN
statements-THEN
EXIT
END IF
statements-2
END DO
What happened in the next DO loops?
INTEGER :: i
DO
IF (i <= 3) EXIT
WRITE(*,*) i
i = i - 1
END DO
INTEGER :: i
i = 5
DO
IF (i < -2) EXIT
WRITE(*,*) i
END DO
INTEGER :: i = 1, j = 5
DO
IF (j < 0) EXIT
WRITE(*,*) i
i = i + 1
END DO
DO control-var = initial-value, final-value, [step-size] statements END DO
INTEGER :: Counter, Init, Final, Step READ(*,*) Init, Final, Step DO Counter = Init, Final, Step ..... END DO
INTEGER :: Count DO Count = -3, 4, 2 WRITE(*,*) Count, Count*Count, Count*Count*Count END DO
INTEGER, PARAMETER :: Init = 3, Final = 5 INTEGER :: Iteration DO Iteration = Init, Final WRITE(*,*) 'Iteration ', Iteration END DO
INTEGER :: a, b, c INTEGER :: List READ(*,*) a, b, c DO List = MAX(a, b, c), MIN(a, b, c), -2 WRITE(*,*) List END DO
INTEGER :: Count, Number, Sum, Input
Sum = 0
DO Count = 1, Number
READ(*,*) Input
Sum = Sum + Input
END DOINTEGER :: Count, Number, Sum, Input
REAL :: Average
Sum = 0
DO Count = 1, Number
READ(*,*) Input
Sum = Sum + Input
END DO
Average = REAL(Sum) / Number
INTEGER :: count
DO count = -3, 4, 0
...
END DO
INTEGER :: a, b, c
DO a = b, c, 3
READ(*,*) a ! the value of a is changed
a = b-c ! the value of a is changed
END DO
INTEGER :: a, b, c, d, e
DO a = b+c, c*d, (b+c)/e
READ(*,*) b ! initial-value is changed
d = 5 ! final-value is changed
e = -3 ! step-size is changed
END DO
INTEGER :: i
DO i = 10, -10
.....
END DO
Given a set of integer input values, write a program to count the number of positive and negative values and compute their sums.
Write a program to calculate the exponential function
See http://en.wikipedia.org/wiki/Exponential_function
Write a program to determine the number of input data items, excluding the negative one at the end, and compute the minimum and the maximum.
Write a program to calculate the GCD using the Euclid's algorithm.
type, DIMENSION( extent ) :: name-1, name-2, ..., name-n
REAL, DIMENSION(-1:1) :: a, Sum
INTEGER, DIMENSION(0:100) :: InputData
INTEGER, PARAMETER :: max = 100 LOGICAL, DIMENSION(1:max ) :: InputData
Declare an array of 100 integers and load even positions with 0 and odd positions with the own value.
program array
INTEGER, DIMENSION(1:100) :: x
DO i = 1, 100
IF (MOD(i,2) == 0) THEN
x(i) = 0
ELSE
x(i) = i
ENDIF
END DO
end program array
Generate an identity matrix.
program matrix
INTEGER, DIMENSION(10, 10) :: x
DO i = 1, 10
DO j = 1, 10
x(i,j) = 0
END DO
x(i,i)= 1
END DO
end program matrix
Swapping the lower and upper diagonal parts (transpose of a matrix)
program transpose INTEGER, DIMENSION(10, 10) :: x INTEGER :: t DO i = 1, 10 DO j = i+1, 10 t = x(i,j) x(i,j) = x(j,i) x(j,i) = t END DO END DO end program transpose
!Another way...
RESULT = TRANSPOSE(MATRIX)
Multiply 2 matrix. Improve the source code...
m
c_i_k = ∑ a_i_k * b_k_i
k=1
program multiply
INTEGER, DIMENSION(10, 10) :: a
INTEGER, DIMENSION(10, 10) :: b
INTEGER, DIMENSION(10, 10) :: c
DO i = 1, 10
DO j = 1, 10
c(i,j) = c(i,j) + a(i,j) * b(j,i)
END DO
END DO
end program multiply
MATMUL(A,B)-->FORTRAN FUNCTION
SUBROUTINE subroutine-name (arg1, arg2, ..., argn)
IMPLICIT NONE
[specification part]
[execution part]
[subprogram part]
END SUBROUTINE subroutine-name
Write a function to calculate the average of 3 numbers.
PROGRAM avgFunction
REAL A,B,C
REAL RESULT
READ *, A,B,C
RESULT = MyAVG(A,B,C)
PRINT *, RESULT
READ *,
END
PROGRAM avgFunction
REAL FUNCTION MyAVG(X,Y,Z)
REAL X,Y,Z,SUM SUM = X + Y + Z
MyAVG = SUM / 3
RETURN
END