(* ============================================================================== * File: GradeCalculator.p * Purpose: Simple program to calculate the percentage and letter grade * Author: Jordan Kidney ( kidney@cpsc.ucalgary.ca ) * Created on: Feb 14, 2006 ============================================================================== *) program GradeCalculator (input,output); (* ========================== MAIN ============================================== *) begin var Mark : real; (* The mark the user got *) var TotalMarks : real; (* Total number of possible marks *) var Percentage : real; (* Used to store the calculated percentage value *) var LetterGrade : char; (* Used to store the letter cade for the mark *) var LetterSign : char; (* Used to store the sign for the letter grade *) var EndLoop : boolean; (* Used as a flag to end loops for error checking *) (* Read in the total marks for the test and make sure * it is greaterr then zero *) EndLoop := false; repeat write('Enter the total number of marks : '); readln(TotalMarks); if (TotalMarks <= 0) then writeln('Error: The number must be greater then zero.') else EndLoop := true until EndLoop = true; (* Read in the mark the user got and make sure * that it is grwater ten or equal to zero and les then the * total number of marks for the test *) EndLoop := false; repeat write('Enter your mark : '); readln(Mark); if ((Mark < 0) or (Mark > TotalMarks)) then writeln('Error: inncorect input') else EndLoop := true until EndLoop = true; (* Calculate the percentage value for the user *) Percentage := (Mark * 100.0)/TotalMarks; (* Find the letter grade based upon the percentage value found *) if (Percentage < 39.0) then begin LetterGrade := 'F'; LetterSign := ' '; end else if (Percentage < 44.0) then begin LetterGrade := 'D'; LetterSign := '-'; end else if (Percentage < 49.0) then begin LetterGrade := 'D'; LetterSign := ' '; end else if (Percentage < 54.0) then begin LetterGrade := 'D'; LetterSign := '+'; end else if (Percentage < 59.0) then begin LetterGrade := 'C'; LetterSign := '-'; end else if (Percentage < 64.0) then begin LetterGrade := 'C'; LetterSign := ' '; end else if (Percentage < 69.0) then begin LetterGrade := 'C'; LetterSign := '+'; end else if (Percentage < 74.0) then begin LetterGrade := 'B'; LetterSign := '-'; end else if (Percentage < 79.0) then begin LetterGrade := 'B'; LetterSign := ' '; end else if (Percentage < 84.0) then begin LetterGrade := 'B'; LetterSign := '+'; end else if (Percentage < 89.0) then begin LetterGrade := 'A'; LetterSign := '-'; end else begin LetterGrade := 'A'; LetterSign := ' '; end; (* Write information to the console *) writeln('You got ', Percentage:0:2, '% on the test which gives you a letter grade of ' ,LetterGrade,LetterSign); end. (* ============================================================================== *)