Dear Sir,

Could you tell me what is wrong of my program?

The question is: write a progrm that totals up a list of exactly 100 numbers and outputs that total to the screen, also show the average of the 100 numbers.

My program:

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
        total, number, count: integer;
        average: real;

begin
        total:=0;
        for count:=1 to 100 do
           begin
              readln(number);
              total:=total+number;
           end;
        writeln(total);
        average:=total/100;
        writeln(average:6:2);
end.

Dani AI

Generated

A short review and practical fixes.

— the original approach will work for small integer-only inputs, but it misses a few robustness points. was right to flag the type/precision issue, and ’s note about output formatting is useful for presentation. To make the program reliable:

  • Accept fractional input by reading into a floating type (Double/Real) and accumulate in a floating-sum, or use Int64 for very large integer sums.
  • Force floating-point division when computing the average (for example divide by 100.0 or cast the sum) so you do not depend on compiler-specific integer-division rules.
  • Validate each input (use TryStrToFloat on Delphi/FreePascal or Val on older Turbo Pascal) and handle fewer-than-100 values or bad lines gracefully.
  • Format output with the desired number of decimals (:0:2, :6:2, etc.).

Example (Delphi/FreePascal) demonstrating validation and safe accumulation:

uses
  SysUtils;

var
  i: Integer;
  s: string;
  value, sum, avg: Double;
begin
  sum := 0.0;
  for i := 1 to 100 do
  begin
    ReadLn(s);
    if not TryStrToFloat(s, value) then
    begin
      Writeln('Invalid number at entry ', i);
      Halt(1);
    end;
    sum := sum + value;
  end;
  avg := sum / 100.0;
  Writeln('Total: ', sum:0:2);
  Writeln('Average: ', avg:0:2);
end.

Notes: if using Turbo Pascal, replace TryStrToFloat with Val. Test with edge cases: negative values, zeros, very large numbers, and fewer-than-100 inputs.

Recommended Answers

All 2 Replies

Dear Sir,

Could you tell me what is wrong of my program?

The question is: write a progrm that totals up a list of exactly 100 numbers and outputs that total to the screen, also show the average of the 100 numbers.

My program:

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
        total, number, count: integer;
        average: real;

begin
        total:=0;
        for count:=1 to 100 do
           begin
              readln(number);
              total:=total+number;
           end;
        writeln(total);
        average:=total/100;
        writeln(average:6:2);
end.

You may need to store average to a double (float), or maybe a variant.

Not sure which pascal you are using.

Hi turbomen.
Your program is working well....where is the problem?
If you want to get the average result as decimals then
write here:
Write('The average is :',average:6:0);
got it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.