{
    PascalAverage.pas

    Sample program in the Pascal programming language.

    Calculates the average of positive integers entered by the user.

    Author:  Mark Young
    Date:    2007-05-03
}

Program Average(Input, Output);

var 
    { Create the variables needed }
    sum:    integer; {Sum of numbers entered so far}
    count:  integer; {How many integers have been entered so far}
    n:      integer; {Latest number entered}

begin
    { Identify myself... }
    write("\n\n\n\n\n\n\n\n\n\n");
    write("Sample Program (Pascal)\n");
    write("=======================\n");
    write("\nFinds the average of positive integers entered by the user.\n");
    write("Enter a negative number to stop input.\n\n\n\n");

    { Initialize variables }
    sum := 0;
    count := 0;

    { Get the first number from the user }
    write("Enter a number: ");  
    read(n);

    { keep going until you get a negative number }
    while n >= 0 do
    begin
        { add the last number to the total }
        sum := sum + n;

        { count that number }
        count := count + 1;

        { get the next number }
        write("Enter a number: ");  
        read(n);
    end;

    { Print the result }
    writeln("\n\nAverage = ", (sum/count):0:1, "\n\n")
end.

