How to resolve the algorithm Multiplication tables step by step in the Fortran programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Multiplication tables step by step in the Fortran programming language
Table of Contents
Problem Statement
Produce a formatted 12×12 multiplication table of the kind memorized by rote when in primary (or elementary) school.
Only print the top half triangle of products.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Multiplication tables step by step in the Fortran programming language
Source code in the fortran programming language
program multtable
implicit none
integer :: i, j, k
write(*, "(a)") " x| 1 2 3 4 5 6 7 8 9 10 11 12"
write(*, "(a)") "--+------------------------------------------------"
do i = 1, 12
write(*, "(i2, a)", advance="no") i, "|"
do k = 2, i
write(*, "(a4)", advance="no") ""
end do
do j = i, 12
write(*, "(i4)", advance="no") i*j
end do
write(*, *)
end do
end program multtable
Cast forth a twelve times table, suitable for chanting at school.
INTEGER I,J !Steppers.
CHARACTER*52 ALINE !Scratchpad.
WRITE(6,1) (I,I = 1,12) !Present the heading.
1 FORMAT (" ×|",12I4,/," --+",12("----")) !Alas, can't do overprinting with underlines now.
DO 3 I = 1,12 !Step down the lines.
WRITE (ALINE,2) I,(I*J, J = 1,12) !Prepare one line.
2 FORMAT (I3,"|",12I4) !Aligned with the heading.
ALINE(5:1 + 4*I) = "" !Scrub the unwanted part.
3 WRITE (6,"(A)") ALINE !Print the text.
END !"One one is one! One two is two! One three is three!...
Cast forth a twelve times table, suitable for chanting at school.
INTEGER I,J !Steppers.
CHARACTER*16 FORMAT !Scratchpad.
WRITE(6,1) (I,I = 1,12) !Present the heading.
1 FORMAT (" ×|",12I4,/," --+",12("----")) !Alas, can't do overprinting with underlines now.
DO 3 I = 1,12 !Step down the lines.
WRITE (FORMAT,2) (I - 1)*4,13 - I !Spacing for omitted fields, count of wanted fields.
2 FORMAT ("(I3,'|',",I0,"X,",I0,"I4)") !The format of the FORMAT statement.
3 WRITE (6,FORMAT) I,(I*J, J = I,12) !Use it.
END !"One one is one! One two is two! One three is three!...
PROGRAM TABLES
IMPLICIT NONE
C
C Produce a formatted multiplication table of the kind memorised by rote
C when in primary school. Only print the top half triangle of products.
C
C 23 Nov 15 - 0.1 - Adapted from original for VAX FORTRAN - MEJT
C
INTEGER I,J,K ! Counters.
CHARACTER*32 S ! Buffer for format specifier.
C
K=12
C
WRITE(S,1) K,K
1 FORMAT(8H(4H0 |,,I2.2,11HI4,/,4H --+,I2.2,9H(4H----)))
WRITE(6,S) (I,I = 1,K) ! Print heading.
C
DO 3 I=1,K ! Step down the lines.
WRITE(S,2) (I-1)*4+1,K ! Update format string.
2 FORMAT(12H(1H ,I2,1H|,,I2.2,5HX,I3,,I2.2,3HI4),8X) ! Format string includes an explicit carridge control character.
WRITE(6,S) I,(I*J, J = I,K) ! Use format to print row with leading blanks, unused fields are ignored.
3 CONTINUE
C
END
PROGRAM TABLES
C
C Produce a formatted multiplication table of the kind memorised by rote
C when in primary school. Only print the top half triangle of products.
C
C 23 Nov 15 - 0.1 - Adapted from original for VAX FORTRAN - MEJT
C 24 Nov 15 - 0.2 - FORTRAN IV version adapted from VAX FORTRAN and
C compiled using Microsoft FORTRAN-80 - MEJT
C
DIMENSION K(12)
DIMENSION A(6)
DIMENSION L(12)
C
COMMON //A
EQUIVALENCE (A(1),L(1))
C
DATA A/'(1H ',',I2,','1H|,','01X,','I3,1','2I4)'/
C
WRITE(1,1) (I,I=1,12)
1 FORMAT(4H0 |,12I4,/,4H --+12(4H----))
C
C Overlaying the format specifier with an integer array makes it possibe
C to modify the number of blank spaces. The number of blank spaces is
C stored as two consecuitive ASCII characters that overlay on the
C integer value in L(7) in the ordr low byte, high byte.
C
DO 3 I=1,12
L(7)=(48+(I*4-3)-((I*4-3)/10)*10)*256+48+((I*4-3)/10)
DO 2 J=1,12
K(J)=I*J
2 CONTINUE
WRITE(1,A)I,(K(J), J = I,12)
3 CONTINUE
C
END
PROGRAM TABLES
C
C Produce a formatted multiplication table of the kind memorised by rote
C when in primary school. Only print the top half triangle of products.
C
C 23 Nov 15 - 0.1 - Adapted from original for VAX FORTRAN - MEJT
C 24 Nov 15 - 0.2 - FORTRAN IV version adapted from VAX FORTRAN and
C compiled using Microsoft FORTRAN-80 - MEJT
C 25 Nov 15 - 0.3 - Microsoft FORTRAN-80 version using a BYTE array
C which makes it easier to understand what is going
C on. - MEJT
C
BYTE A
DIMENSION A(24)
DIMENSION K(12)
C
DATA A/'(','1','H',' ',',','I','2',',','1','H','|',',',
+ '0','1','X',',','I','3',',','1','1','I','4',')'/
C
C Print a heading and (try to) underline it.
C
WRITE(1,1) (I,I=1,12)
1 FORMAT(4H |,12I4,/,4H --+12(4H----))
DO 3 I=1,12
A(13)=48+((I*4-3)/10)
A(14)=48+(I*4-3)-((I*4-3)/10)*10
DO 2 J=1,12
K(J)=I*J
2 CONTINUE
WRITE(1,A)I,(K(J), J = I,12)
3 CONTINUE
C
END
WRITE(1,4) (A(J), J = 1,24)
4 FORMAT(1x,24A1)
You may also check:How to resolve the algorithm Magic squares of doubly even order step by step in the Python programming language
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the Frink programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Inform 7 programming language
You may also check:How to resolve the algorithm Deepcopy step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Sphenic numbers step by step in the Python programming language