Arithmetic in Fortran: Real and Integer Variables

When performing mathematical computations in Fortran, one of the most important considerations is that there are several different types of arithmetic variables. The two most commonly used types are real variables and integer variables.

Important! For an integer variable, the fractional part is not merely zero. The fractional part does not exist:

The type of each variable should be declared at the beginning of the program, right after the program is named. The type declarations have to appear before the first executable statement. Additionally, your variable names should adhere to the following conventions:

Following is a program that illustrates some of the differences between real and integer variables:

      PROGRAM MAIN
      INTEGER I, INTPI, INTE
      REAL X, REALPI, REALE
      I = 1
      X = 1
      PRINT *, 'THE VALUE OF 1 AS A REAL IS', X
      PRINT *, 'THE VALUE OF 1 AS AN INTEGER IS', I
      REALPI = 3.1416
      INTPI = REALPI
      PRINT *, 'THE VALUE OF PI AS A REAL IS', REALPI
      PRINT *, 'THE VALUE OF PI AS AN INTEGER IS', INTPI
      REALE = 2.71828
      INTE = REALE
      PRINT *, 'THE VALUE OF E AS A REAL IS', REALE
      PRINT *, 'THE VALUE OF E AS AN INTEGER IS', INTE
      STOP
      END

Assignment:

(1) Type the program as listed above, compile it and run it.

(2) What happens to the value of pi when it gets converted to an integer?

(3) What happens to the value of e when it gets converted to an integer? Look closely: Is the result rounded to the nearest integer?

(4) Change the values of pi and e to negative numbers (that is, -3.1416 and -2.71828), then re-compile and re-run the program. What happens when the negative values are converted to integers?

Back to main page for programming tutorial