3.1
How many times are the loops, defined by the following statements,
executed:
a) DO I = 2, N/2, 3
with N having the value 2.
Answer: 0
b) DO I = 1, N, 4
with N having the value 2.
Answer: 1
c) DO I = 3, N**2, 5
with N having the value 2.
Answer: 1
d) DO I = 2, N/2, 3
with N having the value 15.
Answer: 2
e) DO I = 1, N, 4
with N having the value 15.
Answer: 4
f) DO I = 3, N**2, 5
with N having the value 15.
Answer: 45
3.2 program
PROGRAM Cartesian
IMPLICIT NONE
REAL :: pi, length, angle, angle_in_radians
pi = ATAN(1.0)*4.0
PRINT *, "Type in length and angle (in degrees)"
READ *, length, angle
angle_in_radians = (angle/180.0)*pi
PRINT *, "(x, y) is (", length*COS(angle_in_radians), &
",", length*SIN(angle_in_radians), " )"
END PROGRAM Cartesian
3.2 output
Type in length and angle (in degrees)
12 77
(x, y) is ( 2.6994123 , 11.6924410 )
Type in length and angle (in degrees)
1000 0
(x, y) is ( 1.0000000E+03 , 0.0000000 )
Type in length and angle (in degrees)
1000 90
(x, y) is ( -4.3711389E-05 , 1.0000000E+03 )
Type in length and angle (in degrees)
20 100
(x, y) is ( -3.4729638 , 19.6961555 )
Type in length and angle (in degrees)
12 437
(x, y) is ( 2.6994104 , 11.6924410 )
3.3 program
PROGRAM Multiples_of_Five
IMPLICIT NONE
INTEGER :: i, num(10), N = 0
READ *, num
DO i = 1,10
IF (num(i) < 0) CYCLE
IF (MOD(num(i),5) == 0) N = N+1
ENDDO
PRINT *, N, ' of the numbers are multiples of 5'
END PROGRAM Multiples_of_Five
3.3 output
5 73 53 12 65 92 39 -11 60 13
3 of the numbers are multiples of 5
0 6 8 8 -2 5 -3 -3 8 3
2 of the numbers are multiples of 5
33 -2 69 -6 -15 93 52 76 56 11
0 of the numbers are multiples of 5
3.4 program
PROGRAM Triangle
IMPLICIT NONE
LOGICAL :: L1, L2
INTEGER :: side1, side2, side3
PRINT *, "Type in the three sides:"
READ *, side1, side2, side3
Outer: IF (2*MAX(side1, side2, side3) >= side1+side2+side3) THEN
PRINT *, "Not a Triangle"
ELSE Outer
L1 = (side1 == side2)
L2 = (side2 == side3)
IF (L1 .AND. L2) THEN
PRINT *, "Equilateral"
ELSE IF (L1 .OR. L2 .OR. side1 == side3) THEN
PRINT *, "Isoceles"
ELSE
PRINT *, "Scalene"
END IF
END IF Outer
END PROGRAM Triangle
3.4 output
Type in the three sides:
1 1 1
Equilateral
Type in the three sides:
2 2 1
Isoceles
Type in the three sides:
1 1 0
Not a Triangle
Type in the three sides:
3 4 5
Scalene
Type in the three sides:
3 2 1
Not a Triangle
Type in the three sides:
1 2 4
Not a Triangle
3.5 program
PROGRAM Math_Magic
IMPLICIT NONE
INTEGER :: num, tnum
outer: DO
PRINT *, "Type in your number (0 terminates)"
READ *, num
IF (num .LE. 0) EXIT
inner: DO
tnum = num/2
IF (2*tnum .EQ. num) THEN ! num is even
num = tnum
ELSE ! num is odd
num = 3*num+1
END IF
PRINT *, num
IF (num == 1) THEN
PRINT *, "Sequence finishes nicely"
EXIT
ELSE IF (num == 13) THEN
PRINT *, "Yoiks, extreme bad luck encountered"
EXIT outer
END IF
END DO inner
END DO outer
END PROGRAM Math_Magic
3.5 output
7
22 11 34 17 52 26 13 Yoiks, extreme bad luck encountered
106
53 160 80 40 20 10 5 16 8 4 2 1 Sequence finishes nicely
46
23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1 Sequence finishes nicely
3
10 5 16 8 4 2 1 Sequence finishes nicely
3.6 program
PROGRAM colors
IMPLICIT NONE
CHARACTER(LEN=10) :: color
INTEGER :: nred = 0, ngreen = 0, nblue = 0, nyellow = 0, &
nblack = 0, nwhite = 0, nother = 0
DO
READ *, color
color = TRIM(ADJUSTL(color)) ! gets rid of leading
! and trailing spaces
IF (color == 'red') THEN
nred = nred+1
ELSE IF (color == 'green') THEN
ngreen = ngreen + 1
ELSE IF (color == 'blue') THEN
nblue = nblue + 1
ELSE IF (color == 'yellow') THEN
nyellow = nyellow + 1
ELSE IF (color == 'black') THEN
nblack = nblack + 1
ELSE IF (color == 'white') THEN
nwhite = nwhite + 1
ELSE IF (color == 'blank') THEN
EXIT
ELSE
nother = nother+1
END IF
END DO
PRINT *, 'red =', nred, ' green =', ngreen, &
' blue =', nblue, ' yellow =', nyellow, &
' black =', nblack, ' white =', nwhite, &
' other =', nother
END PROGRAM colors
3.6 output
red = 1 green = 3 blue = 0 yellow = 1 black = 4 white = 1 other = 2