PROGRAM Grades
   IMPLICIT NONE

   INTEGER   :: Score1, Score2, Score3, Score4, Score5
   INTEGER   :: TotalScore, Add 
   CHARACTER :: Grade


! Read in the scores and make sure they are in range

   READ(*,*) Score1, Score2, Score3, Score4, Score5

   IF ( Score1 < 0 .OR. Score2 < 0 .OR. Score3 < 0 .OR. &
        Score4 < 0 .OR. Score5 < 0) THEN
      WRITE(*,*) 'At least on of the Scores was negative.'
      WRITE(*,*) 'If this is not correct run the progam again.'
   END IF 
   IF ( Score1 > 100 .OR. Score2 > 100 .OR. Score3 > 100 .OR. &
        Score4 > 100 .OR. Score5 > 100) THEN
      WRITE(*,*) 'At least on of the Scores was over 100.'
      WRITE(*,*) 'If this is not correct run the progam again.'
   END IF 

! Calculate the Total Score

   TotalScore = (Score1 + Score2 + Score3 + Score4 + Score5) 
   IF (MOD ( TotalScore, 5) >= 3 ) THEN 
       Add = 1
   ELSE 
       Add = 0
   END IF
   TotalScore = TotalScore / 5 + Add

! Determine Letter Grade

   SELECT CASE (TotalScore)
      CASE (:59) 
         Grade = 'F'
      CASE (60:69)
         Grade = 'D'
      CASE (70:79) 
         Grade = 'C'
      CASE (80:89) 
         Grade = 'B'
      CASE DEFAULT
         Grade = 'A'
   END SELECT
  
   IF (TotalScore > 100 .OR. TotalScore < 0) THEN
      Grade = '*'
      WRITE(*,*) '******* Total Score out of Range *******'
   END IF


! Write out the scores and the grade 

   WRITE(*,*)
   WRITE(*,*) 'Test 1 grade = ' , Score1
   WRITE(*,*) 'Test 2 grade = ' , Score2
   WRITE(*,*) 'Test 3 grade = ' , Score3
   WRITE(*,*) 'Test 4 grade = ' , Score4
   WRITE(*,*) 'Test 5 grade = ' , Score5
   WRITE(*,*) '==============================='
   WRITE(*,*) 'Total Grade  = ' , TotalScore
   WRITE(*,*) '==============================='
   WRITE(*,*) 'Letter Grade = ' , Grade

END PROGRAM Grades
