Computing Cubes

Problem Statement

Write a program to compute the cubes of 1, 2, 3, ..., 10 in both INTEGER and REAL types. It is required to write a function intCube() for computing the cube of an integer and a function realCube() for computing the cube of a real.

Solution

! -----------------------------------------------------
!    This program display the cubes of INTEGERs and 
! REALs.  The cubes are computed with two functions:
! intCube() and realCube().
! -----------------------------------------------------

PROGRAM  Cubes
   IMPLICIT   NONE

   INTEGER, PARAMETER :: Iterations = 10
   INTEGER            :: i
   REAL               :: x

   DO i = 1, Iterations
      x = i
      WRITE(*,*)  i, x, intCube(i), realCube(x)
   END DO

CONTAINS

! -----------------------------------------------------
! INTEGER FUNCTION intCube() :
!    This function returns the cube of the argument.
! -----------------------------------------------------

   INTEGER  FUNCTION  intCube(Number)
      IMPLICIT   NONE

      INTEGER, INTENT(IN) :: Number

      intCube = Number*Number*Number
   END FUNCTION  intCube

! -----------------------------------------------------
! REAL FUNCTION realCube() :
!    This function returns the cube of the argument.
! -----------------------------------------------------

   REAL FUNCTION  realCube(Number)
      IMPLICIT  NONE

      REAL, INTENT(IN) :: Number

      realCube = Number*Number*Number
   END FUNCTION  realCube

END PROGRAM  Cubes
Click here to download this program.

Program Input and Output

The following is the output from the above program.
 1,  1.,  1,  1.
 2,  2.,  8,  8.
 3,  3.,  27,  27.
 4,  4.,  64,  64.
 5,  5.,  125,  125.
 6,  6.,  216,  216.
 7,  7.,  343,  343.
 8,  8.,  512,  512.
 9,  9.,  729,  729.
 10,  10.,  1000,  1000.

Discussion