Pascal: Variable Decimal Places

program ex4(input,output);

var a, b, sum : real;
      dp : integer;
begin
  writeln ('Enter a real number');
  readln (a);
  writeln ('Enter another real number');
  readln (b);
  writeln ('Enter an integer for the number of decimal places in the output');
  readln (dp);
  sum:=a + b;
  writeln ('Sum is: ', sum: 8: dp);
  readln;
end.

We declare 3 real numbers, 3 for input and 1 for the sum. We read the numbers from the keyboard and then use the + to add them. In the output we use 8 for the width of the output and 2 for the number of decimal places.

Back to questions