In data processing, the year, month and day information are usually written as yyyymmdd, where the first four digits are Year, the fifth and sixth digits are Month, and the last two digits are Day. For example, 19710428 means April 8, 1971, and 20000101 means January 1, 2000.
Write a program to read an integer in the form of yyyymmdd and
extract the values of Year, Month and Day.
Do it with an external subroutine.
! --------------------------------------------------------------------
! PROGRAM YYYYMMDDConversion:
! This program uses an external subroutine Conversion() to convert
! an integer value in the form of YYYYMMDD to Year, Month and Day.
! --------------------------------------------------------------------
PROGRAM YYYYMMDDConversion
IMPLICIT NONE
INTERFACE ! interface block
SUBROUTINE Conversion(Number, Year, Month, Day)
INTEGER, INTENT(IN) :: Number
INTEGER, INTENT(OUT) :: Year, Month, Day
END SUBROUTINE Conversion
END INTERFACE
INTEGER :: YYYYMMDD, Y, M, D
DO ! loop until a zero is seen
WRITE(*,*) "A YYYYMMDD (e.g., 19971027) please (0 to stop) -> "
READ(*,*) YYYYMMDD ! read in the value
IF (YYYYMMDD == 0) EXIT ! if 0, then bail out
CALL Conversion(YYYYMMDD, Y, M, D) ! do conversation
WRITE(*,*) "Year = ", Y ! display results
WRITE(*,*) "Month = ", M
WRITE(*,*) "Day = ", D
WRITE(*,*)
END DO
END PROGRAM YYYYMMDDConversion
! --------------------------------------------------------------------
! SUBROUTINE Conversion():
! This external subroutine takes an integer input Number in the
! form of YYYYMMDD and convert it to Year, Month and Day.
! --------------------------------------------------------------------
SUBROUTINE Conversion(Number, Year, Month, Day)
IMPLICIT NONE
INTEGER, INTENT(IN) :: Number
INTEGER, INTENT(OUT) :: Year, Month, Day
Year = Number / 10000
Month = MOD(Number, 10000) / 100
Day = MOD(Number, 100)
END SUBROUTINE Conversion
Click here to download this program.
A YYYYMMDD (e.g., 19971027) please (0 to stop) -> 19971026 Year = 1997 Month = 10 Day = 26 A YYYYMMDD (e.g., 19971027) please (0 to stop) -> 20160131 Year = 2016 Month = 1 Day = 31 A YYYYMMDD (e.g., 19971027) please (0 to stop) -> 19010103 Year = 1901 Month = 1 Day = 3 A YYYYMMDD (e.g., 19971027) please (0 to stop) -> 0