Pascal: Choosing Justification Style

program ex6(input,output);

var a, b, c, d : real;
      justification : char;
begin
  writeln ('Enter a real number');
  readln (a);
  writeln ('Enter another real number');
  readln (b);
  writeln ('Enter another real number');
  readln (c);
  writeln ('Enter the last real number');
  readln (d);
  writeln ('Enter l to left justify output, r to right justify:');
  readln (justification);
  if (justification = 'l') then 
  begin
    writeln ('Left justified: ');
    writeln (a: 2:2);
    writeln (b: 2:2);
    writeln (c: 2:2);
    writeln (d: 2:2);
  end
  else
  if (justification='r') then
  begin
    writeln ('Right justified: ');
    writeln (a: 8:2);
    writeln (b: 8:2);
    writeln (c: 8:2);
    writeln (d: 8:2);
  end
  else 
    writeln('Wrong character entered');
  readln;
end.

We declare 4 real numbers. We read the numbers from the keyboard and then give the user a choice of outputting them left or right justified. We use an if statement to examine the user's choice and output the numbers accordingly. If the user enters something other than 'l' or 'r' the program outputs an error message (wrong input).. 

The if statement gives us a means to test for potential errors, for example if divisor <> 0 then perform division... 

Back to questions