Real Variables

Numbers having a decimal part are known in Computing as real numbers. The default representation of a real number is scientific notation, shown in the first equation in the screenshot. You can change it to fixed point decimal representation by supplying the maximum number of characters in the display (the field width) and the number of places of decimals. The colon is used as the separator. In the program following its screenshot Quotient : 6 : 2 displays the answer to two places of decimals and allows a total of 6 characters to be displayed.



program RealNumber;
  Num1, Num2  : integer;
  Quotient : real;
  write('Please enter a whole number between 1 and 100: ');
  write('Please enter another whole number between 1 and 100: ');
  Quotient := Num1 / Num2;
  writeln('Num1 / Num2 = ', Quotient);
  writeln('Num1 / Num2 = ', Quotient : 6 : 2);

When you have covered more language features you should be able to write code that formats the real number according to its value and positions the number exactly where you want it in relation to your text.

Features introduced:
  • Real variables
  • Use of the operator / for division
  • Formatting a real number
Programming - a skill for life!

The various types of constants and variables and how to use them