Skip to content

Instantly share code, notes, and snippets.

@nilforooshan
Last active March 30, 2019 10:05
Show Gist options
  • Save nilforooshan/c4b7c3b54f2ee0c5d88e6bbf2ffad0bc to your computer and use it in GitHub Desktop.
Save nilforooshan/c4b7c3b54f2ee0c5d88e6bbf2ffad0bc to your computer and use it in GitHub Desktop.
f90: Worried about your age? Calculate it in days! Calculations according to: http://mathforum.org/library/drmath/view/59234.html *.exe and *.out are the Windows and Linux executables.
PROGRAM ageindays
IMPLICIT NONE
! Declarations
INTEGER:: birth, cc1, yy1, mm1, dd1, today, cc2, yy2, mm2, dd2
INTEGER:: since16000301, age
CHARACTER(8):: date, exiit
! Opening prints
PRINT*,
PRINT*,
PRINT*, 'This program is written by Mohammad A. Nilforooshan.'
PRINT*, 'All rights reserved.'
PRINT*, 'http://sites.google.com/site/mannprofile/'
! Ask the current date from the computer
CALL DATE_AND_TIME(date)
READ (date,*) today
cc2=INT(FLOOR(today/1000000.))
yy2=INT(FLOOR(today/10000.)-cc2*100)
mm2=INT(FLOOR(today/100.)-cc2*10000-yy2*100)
dd2=today-cc2*1000000-yy2*10000-mm2*100
! Ask for the birth date
PRINT*,
PRINT*,
PRINT*, 'Insert your birth date (yyyymmdd).'
READ*, birth
cc1=INT(FLOOR(birth/1000000.))
yy1=INT(FLOOR(birth/10000.)-cc1*100)
mm1=INT(FLOOR(birth/100.)-cc1*10000-yy1*100)
dd1=birth-cc1*1000000-yy1*10000-mm1*100
! Check for a valid birth date
IF (birth>today) THEN
PRINT*,
PRINT*,
PRINT*, 'Error: negative age!'
STOP
END IF
IF (cc1<0 .OR. cc1>29) THEN
PRINT*,
PRINT*,
PRINT*, 'Error: invalid date'
STOP
END IF
IF (mm1==0 .OR. mm1>12) THEN
PRINT*,
PRINT*,
PRINT*, 'Error: invalid date'
STOP
END IF
IF (dd1==0 .OR. dd1>31) THEN
PRINT*,
PRINT*,
PRINT*, 'Error: invalid date'
STOP
END IF
IF (dd1>28 .AND. mm1==2) THEN
PRINT*,
PRINT*,
PRINT*, 'Error: invalid date'
STOP
END IF
IF (dd1==31 .AND. (mm1==4 .OR. mm1==6 .OR. mm1==9 .OR. mm1==11)) THEN
PRINT*,
PRINT*,
PRINT*, 'Error: invalid date'
STOP
END IF
! Calculate the age
age=since16000301(cc2,yy2,mm2,dd2)-since16000301(cc1,yy1,mm1,dd1)
! Print the result on the screen
PRINT*,
PRINT*,
PRINT*, 'You are',age,'days old.'
PRINT*,
PRINT*,
PRINT*, 'Press any key to exit.'
READ*, exiit
IF (exiit=='a') THEN
GO TO 105
END IF
105 END PROGRAM ageindays
! Introducing the since16000301 function
INTEGER FUNCTION since16000301(cc,yy,mm,dd)
INTEGER:: cc, yy, mm, dd
mm=mm-2
since16000301=365*(100*(cc-16)+yy)+24*cc+INT(cc/4.)+INT(yy/4.)+30*mm &
+INT((3*mm-1)/5.)+dd-419
RETURN
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment