How to produce a sequential count in octal, starting at zero in Pascal

2 Answers

0 votes
// Sequential octal counter
 
program OctalCount;

uses SysUtils;

function ToOctal(n: Integer): String;
var
  s: String;
  digit: Integer;
begin
  s := '';
  repeat
    digit := n mod 8;
    s := Chr(Ord('0') + digit) + s;
    n := n div 8;
  until n = 0;
  ToOctal := s;
end;
 
var
  i: Integer;
begin
  for i := 0 to 31 do
    writeln(IntToStr(i), ' = ', ToOctal(i));
end.



(*
run:

0 = 0
1 = 1
2 = 2
3 = 3
4 = 4
5 = 5
6 = 6
7 = 7
8 = 10
9 = 11
10 = 12
11 = 13
12 = 14
13 = 15
14 = 16
15 = 17
16 = 20
17 = 21
18 = 22
19 = 23
20 = 24
21 = 25
22 = 26
23 = 27
24 = 30
25 = 31
26 = 32
27 = 33
28 = 34
29 = 35
30 = 36
31 = 37

*)

 



answered Mar 24 by avibootz
edited Mar 24 by avibootz
0 votes
// Fixed‑width octal (e.g., always 3 digits)

program OctalCountFixed;

uses SysUtils;

function ToOctal(n: Integer): String;
var
  s: String;
  digit: Integer;
begin
  s := '';
  repeat
    digit := n mod 8;
    s := Chr(Ord('0') + digit) + s;
    n := n div 8;
  until n = 0;

  while Length(s) < 3 do
    s := '0' + s;

  ToOctal := s;
end;

var
  i: Integer;
begin
  for i := 0 to 64 do
    writeln(IntToStr(i), ' = ', ToOctal(i));
end.



(*
run:

0 = 000
1 = 001
2 = 002
3 = 003
4 = 004
5 = 005
6 = 006
7 = 007
8 = 010
9 = 011
10 = 012
11 = 013
12 = 014
13 = 015
14 = 016
15 = 017
16 = 020
17 = 021
18 = 022
19 = 023
20 = 024
21 = 025
22 = 026
23 = 027
24 = 030
25 = 031
26 = 032
27 = 033
28 = 034
29 = 035
30 = 036
31 = 037
32 = 040
33 = 041
34 = 042
35 = 043
36 = 044
37 = 045
38 = 046
39 = 047
40 = 050
41 = 051
42 = 052
43 = 053
44 = 054
45 = 055
46 = 056
47 = 057
48 = 060
49 = 061
50 = 062
51 = 063
52 = 064
53 = 065
54 = 066
55 = 067
56 = 070
57 = 071
58 = 072
59 = 073
60 = 074
61 = 075
62 = 076
63 = 077
64 = 100

*)

 



answered Mar 24 by avibootz
edited Mar 24 by avibootz
...