1

enter image description here

How do I make a normal multiplication table as in C/C++?
When I use Fortran, there's a very huge gap between the number and the value (look at the image)
.

program main
    implicit none
    integer(kind = 8) :: a, b
    print *, "Input a number : "
    read(*,*) a
    
    do b = 1, 10
        print *, a, " * ", b, " = ", a*b
    end do
    print *, " "
    do b = 1, 10
        print *, a, " ** ", b, " = ", a**b
    end do
    ! system("pause")
end program main
John Alexiou
  • 28,472
  • 11
  • 77
  • 133

1 Answers1

2

Use the g0 format specifier to create the shortest possible representation of a number.

program FortranMulTableConsole
use, intrinsic :: iso_fortran_env
implicit none

integer(kind=int64) :: a, b
print *, "Input a number : "
read(*,*) a

do b = 1, 10
    print '(g0," * ",g0," = ",g0)', a, b, a*b
end do
print *, " "
do b = 1, 10
    print '(g0," ** ",g0," = ",g0)', a, b, a**b
end do

! system("pause")
contains

end program FortranMulTableConsole

results in

 Input a number :
8
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80

8 ** 1 = 8
8 ** 2 = 64
8 ** 3 = 512
8 ** 4 = 4096
8 ** 5 = 32768
8 ** 6 = 262144
8 ** 7 = 2097152
8 ** 8 = 16777216
8 ** 9 = 134217728
8 ** 10 = 1073741824
John Alexiou
  • 28,472
  • 11
  • 77
  • 133