Advertisement
Guest User

Untitled

a guest
Aug 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. program cylinder
  2.  
  3. ! Calculate the surface area of a cylinder.
  4. !
  5. ! Declare variables and constants.
  6. ! constants=pi
  7. ! variables=radius squared and height
  8.  
  9.   implicit none    ! Require all variables to be explicitly declared
  10.  
  11.   integer :: ierr
  12.   character(1) :: yn
  13.   real :: radius, height, area
  14.   real, parameter :: pi = 3.141592653589793
  15.  
  16.   interactive_loop: do
  17.  
  18. !   Prompt the user for radius and height
  19. !   and read them.
  20.  
  21.     write (*,*) 'Enter radius and height.'
  22.     read (*,*,iostat=ierr) radius,height
  23.  
  24. !   If radius and height could not be read from input,
  25. !   then cycle through the loop.
  26.  
  27.     if (ierr /= 0) then
  28.       write(*,*) 'Error, invalid input.'
  29.       cycle interactive_loop
  30.     end if
  31.  
  32. !   Compute area.  The ** means "raise to a power."
  33.  
  34.     area = 2*pi * (radius**2 + radius*height)
  35.  
  36. !   Write the input variables (radius, height)
  37. !   and output (area) to the screen.
  38.  
  39.     write (*,'(1x,a7,f6.2,5x,a7,f6.2,5x,a5,f6.2)') &
  40.       'radius=',radius,'height=',height,'area=',area
  41.  
  42.     yn = ' '
  43.     yn_loop: do
  44.       write(*,*) 'Perform another calculation? y[n]'
  45.       read(*,'(a1)') yn
  46.       if (yn=='y' .or. yn=='Y') exit yn_loop
  47.       if (yn=='n' .or. yn=='N' .or. yn==' ') exit interactive_loop
  48.     end do yn_loop
  49.  
  50.   end do interactive_loop
  51.  
  52. end program cylinder
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement