I've rewritten your code without the loop (that's however easy to insert again, if you like it) using a subroutine for formatting the output:
Code:
program cylinder
implicit none
real :: radius, height, area
integer :: ilog10_r, ilog10_h, ilog10_a ! the integer parts of the common logarithms are useful
! for formatting the output
real, parameter :: pi = 3.141592654
print *, "Enter the radius and the height: "
read *, radius, height
area = 2 * pi * (radius**2 + radius * height)
ilog10_r = int(log10(radius))
ilog10_h = int(log10(height))
ilog10_a = int(log10(area))
call pprint (radius, ilog10_r, height, ilog10_h, area, ilog10_a)
end program cylinder
subroutine pprint (r, il_r, h, il_h, a, il_a)
implicit none
real :: r, h, a
integer :: il_r, il_h, il_a
character (2) :: r_fmt
character (2) :: h_fmt
character (2) :: a_fmt
! for the trick of how to convert numbers to strings, see here:
! http://www.eng-tips.com/viewthread.cfm?qid=37205
write (r_fmt, '(i2)') il_r + 4
write (h_fmt, '(i2)') il_h + 4
write (a_fmt, '(i2)') il_a + 4
write (*, '(1xa7f'//r_fmt//'.2,5xa7f'//h_fmt//'.2,5xa7f'//a_fmt//'.2)') &
"radius=", r, "height=", h, "area =", a
end subroutine pprint
Bookmarks