Wednesday, December 5, 2012

Subroutine subprogram

** Write and run a Fortran program to compute the total surface area and volume of a rectangular box whose width is 10.5 , depth is 8.5 and height is 5.38.
1234567890
                 write(*,*) 'Enter the value of x y and z'
         Read(*,*) x,y,z
         call cal(x,y,z,v,s)
         write(*,*) 'The results are ', s,'and',v
         stop
         end
         subroutine cal(x,y,z,v,s)
         s=2*(x*y+y*z+z*x)
         v=x*y*z
         Return
         End

Description:
   write(*,*) 'Enter the value of x y and z'
                 (to show the above line to the output)
         Read(*,*) x,y,z
                   ( to read the width , depth and height)
         call cal(x,y,z,v,s)
                   ( we have to take the result from subprogram by call statement) and (cal(x,y,z,v,s) is the name of subprogram)
         write(*,*) 'The results are ', s,'and',v
(to show the result in output)
         stop
         end
         subroutine cal(x,y,z,v,s)
                      ( cal(x,y,z,v,s) is the name of the subroutine subprogram and in main program we have to call subprogram by this name)
         s=2*(x*y+y*z+z*x)
         v=x*y*z
         Return
(every subprogram must include a return statement)
         End
Here if we found in subroutine i.e cal(x,y,z,v,s) . we see that the components of the subroutine subprogram are x(which is weight ) ,y(which is depth) and z( which is height) of the rectangular. V is (         v=x*y*z) and S is ( s=2*(x*y+y*z+z*x)) . So to make a subroutine subprogram first of all we need to write a subroutine statement and then we have to see the component that we need i.e x , y , z ,v , and s. 
**The values for the volume and surface area of a spherical segment can be determined from the relationships

        
Where H and L are given below.
H                 L
3.50            10.50
3.50            15.00
2.25            8.50
3.00            9.75
Write a program that will read the data for H and L . For each set of values compute and print the values of the volume and surface area. Print the given and computed values under appropriate headings.
Solution:
1234567890
                  Real L,H
        Do k = 1,4
        Write(*,*) 'Enter the values of H and L'
        Read(*,*) H , L
        Call MAK(H,L,V,A)
         Write(*,*) '  H     L        V        A'
         Write(*,*) H,L,V,A
                 End do
          End
        subroutine  MAK(H,L,V,A)
             Pi=3.1416
        q=(L**2+4*h**2)/(8*H)
        w=H/3
        V=pi*H**2*(q-w)
        A=(Pi*(2*H**2+L**2))/2
        Return
        End    
Here the output will be like this
Output:
1234567890
                 Enter the values of H and L
3.50                                                      (then press ENTER)
10.50                                                    (press ENTER)
   H     L        V        A
  3.5  10.5  22.4493504  38.4846001
 Enter the values of H and L
3.50                                                     (press ENTER)
15.00                                                   (press ENTER)
   H     L        V        A
  3.5  15.  22.4493504  38.4846001
 Enter the values of H and L
2.25                                                     (press ENTER)      
8.50                                                     (press ENTER)
   H     L        V        A
  2.25  8.5  5.96413088  15.9043493
 Enter the values of H and L
3.00                                                    (press ENTER)
9.75                                                    (press ENTER)
   H     L        V        A
  3.  9.75  14.1371994  28.2743988

No comments:

Post a Comment