Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Github repo cloud-examples: https://github.com/sagemath/cloud-examples

Views: 8055
License: MIT
1
program cylinder
2
3
! source: https://en.wikibooks.org/wiki/Fortran/Fortran_examples
4
! Calculate the surface area of a cylinder.
5
!
6
! Declare variables and constants.
7
! constants=pi
8
! variables=radius squared and height
9
10
implicit none ! Require all variables to be explicitly declared
11
12
integer :: ierr
13
character(1) :: yn
14
real :: radius, height, area
15
real, parameter :: pi = 3.141592653589793
16
17
interactive_loop: do
18
19
! Prompt the user for radius and height
20
! and read them.
21
22
write (*,*) 'Enter radius and height.'
23
read (*,*,iostat=ierr) radius,height
24
25
! If radius and height could not be read from input,
26
! then cycle through the loop.
27
28
if (ierr /= 0) then
29
write(*,*) 'Error, invalid input.'
30
cycle interactive_loop
31
end if
32
33
! Compute area. The ** means "raise to a power."
34
35
area = 2 * pi * (radius**2 + radius*height)
36
37
! Write the input variables (radius, height)
38
! and output (area) to the screen.
39
40
write (*,'(1x,a7,f6.2,5x,a7,f6.2,5x,a5,f6.2)') &
41
'radius=',radius,'height=',height,'area=',area
42
43
yn = ' '
44
yn_loop: do
45
write(*,*) 'Perform another calculation? y[n]'
46
read(*,'(a1)') yn
47
if (yn=='y' .or. yn=='Y') exit yn_loop
48
if (yn=='n' .or. yn=='N' .or. yn==' ') exit interactive_loop
49
end do yn_loop
50
51
end do interactive_loop
52
53
end program cylinder
54