Skip to main content
Engineering LibreTexts

10.9: Example

  • Page ID
    54293
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    This example will read a date from the user (month, day, and year, on the same line), determine the day of week (for that month/day/year). Then, the program will display the original input date (numeric form) and the formatted date. The original input date will be displayed with the applicable fields including the leading 0's (i.e., 01/01/2010).

    Understand the Problem

    For this problem, we will read the three numbers for the date from the user. The verification of the date information is left as an exercise.

    To calculate the day on which a particular date falls, the following algorithm may be used (the divisions are integer divisions):

    a = (14 - month) / 12
    y = year - a
    m = month + 12*a - 2
    daynum = [ date + y + y/4 - y/100 + y/400 + (31*m/12) ] mod 7
    

    The value of daynum is 0 for a Sunday, 1 for a Monday, 2 for a Tuesday, etc.

    Create the Algorithm

    For this problem, first we will need to read the date. The verification of the date entered and error checking is left as an exercise. Then, the original input date can be displayed, in numeric form, formatted appropriately. For a date, this would mean two digits for the month, a “/”, two digits for the day, a “/”, and four digits for the year. When the day is only one digit, for example 5, it is customary to display a “05” so the program will ensure this occurs.

    ! declare variables
    !     integer -> month, day, year
    !     integer -> a, y, m, daynum (for calculations)
    ! display initial header
    ! prompt for month, day, and year
    ! read month, day, and year
    ! display formatted numeric month/day/year
    

    Then the program can calculate the day of the week (based on the formula) and convert the resulting number (0-6) into a date string and display the result.

    ! calculate day of week
    ! convert day of week (0-6) to string
    ! convert month (1-12) to string
    ! display formatted string for day, month, and year
    

    For convenience, the steps are written as program comments.

    Implement the Program

    Based on the algorithm, the below program could be created.

    program dateFormatter
    
    ! declare variables
    implicit none
    integer :: month, day, year
    integer :: a, m, y, d
    character(9) :: amonth, day_of_week
    
    ! ----------
    ! display initial header
        write (*,*) "Date Formatting Example"
    
    ! prompt for  month, day, and year
        write (*,'(a)',advance="no") "Date (month, day, year):"
    
    ! read month, day, and year
        read (*,*) month, day, year
    
    ! ----------
    ! display formatted numeric month/day/year
    
        write (*,*) "----------------------------------------"
        write (*,*) "Input Date: "
        write (*,'(5x, i2.2, a, i2.2, a, i4)') month, "/", &
            day, "/", year
    
    ! ----------
    ! calculate day of week
    
        a = (14 - month) / 12
        y = year - a
        m = month + 12 * a - 2
        d = mod ( (day + y + y/4 - y/100 + y/400 + (31*m/12)), 7)
    
    ! ----------
    ! convert day-of-week integer to day-of-week string 
    
        select case (d)
            case (0)
                day_of_week = "Sunday   "
            case (1)
                day_of_week = "Monday   "
            case (2)
                day_of_week = "Tuesday  "
            case (3)
                day_of_week = "Wednesday"
            case (4)
                day_of_week = "Thursday "
            case (5)
                day_of_week = "Friday   "
            case (6)
                day_of_week = "Saturday "
        end select
    
    ! ----------
    ! convert month (1-12) to string
        select case (month)
            case (1)
                amonth = "January  "
            case (2)
                amonth = "February "
            case (3)
                amonth = "March    "
            case (4)
                amonth = "April    "
            case (5)
                amonth = "May      "
            case (6)
                amonth = "June     "
            case (7)
                amonth = "July     "
            case (8)
                amonth = "August   "
            case (9)
                amonth = "September"
            case (10)
                amonth = "October  "
            case (11)
                amonth = "November "
            case (12)
                amonth = "December "
        end select
    
    ! ----------
    ! display formatted string for day, month, and year
    
        write (*,'(/a)') "Formatted Date:"
        write (*,'(5x, a, a, a, 1x, i2.2, a, i4/)') &
             trim(day_of_week), ", ", trim(amonth), &
             day, ", ", year
    
    end program dateFormatter
    

    The spacing and indentation is not required, but help to make the program more readable. The trim() intrinsic function removes any trailing spaces from the input string. Additional information regarding handling character data types is provided in the following section.

    Test/Debug the Program

    For this problem, the testing would be to ensure that the output formatting is correct. Since there is no error checking on the input, only correct dates should be entered. Test the program on a series of different input values and verify that the output is correct for those input values.


    This page titled 10.9: Example is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?