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

2 Answers

0 votes
// Simple loop using std::oct
  
#include <iostream>

int main() {
    for (int i = 0; i < 20; i++) {
        std::cout << std::dec << i        // print decimal
                  << " = "
                  << std::oct << i        // print octal
                  << "\n";
    }
}

  
/*
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
 
*/

 



answered Mar 23 by avibootz
edited Mar 24 by avibootz
0 votes
// If we need fixed width (e.g., always 3 digits)
 
#include <iostream>
#include <iomanip>
 
int main() {
    for (int i = 0; i < 65; i++) {
         std::cout << std::dec << i        // print decimal
                   << " = "
                   << std::setw(3) << std::setfill('0') 
                   << std::oct << i        // print octal
                   << "\n";
    }
}
 
 
 
/*
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 23 by avibootz
edited Mar 24 by avibootz
...