Deitel C++ How To Program 9th Edition Chapter 5 Exercise 5.18

14 Jul 2024 - Syed Muhammad Shahrukh Hussain

Write a program to display number system table (decimal, binary, octal and Hexadecimal).

Terminal

Show decimal equivalent 1-256
Decimal    Binary          Octal    Hexadecimal
1              1              1              1
2             10              2              2
3             11              3              3
4            100              4              4
5            101              5              5
6            110              6              6
7            111              7              7
8           1000             10              8
9           1001             11              9
10           1010             12              A
11           1011             13              B
12           1100             14              C
13           1101             15              D
14           1110             16              E
15           1111             17              F
16          10000             20             10
17          10001             21             11
18          10010             22             12
19          10011             23             13
20          10100             24             14
21          10101             25             15
22          10110             26             16
23          10111             27             17
24          11000             30             18
25          11001             31             19
26          11010             32             1A
27          11011             33             1B
28          11100             34             1C
29          11101             35             1D
30          11110             36             1E
31          11111             37             1F
32         100000             40             20
33         100001             41             21
34         100010             42             22
35         100011             43             23
36         100100             44             24
37         100101             45             25
38         100110             46             26
39         100111             47             27
40         101000             50             28
41         101001             51             29
42         101010             52             2A
43         101011             53             2B
44         101100             54             2C
45         101101             55             2D
46         101110             56             2E
47         101111             57             2F
48         110000             60             30
49         110001             61             31
50         110010             62             32
51         110011             63             33
52         110100             64             34
53         110101             65             35
54         110110             66             36
55         110111             67             37
56         111000             70             38
57         111001             71             39
58         111010             72             3A
59         111011             73             3B
60         111100             74             3C
61         111101             75             3D
62         111110             76             3E
63         111111             77             3F
64        1000000            100             40
65        1000001            101             41
66        1000010            102             42
67        1000011            103             43
68        1000100            104             44
69        1000101            105             45
70        1000110            106             46
71        1000111            107             47
72        1001000            110             48
73        1001001            111             49
74        1001010            112             4A
75        1001011            113             4B
76        1001100            114             4C
77        1001101            115             4D
78        1001110            116             4E
79        1001111            117             4F
80        1010000            120             50
81        1010001            121             51
82        1010010            122             52
83        1010011            123             53
84        1010100            124             54
85        1010101            125             55
86        1010110            126             56
87        1010111            127             57
88        1011000            130             58
89        1011001            131             59
90        1011010            132             5A
91        1011011            133             5B
92        1011100            134             5C
93        1011101            135             5D
94        1011110            136             5E
95        1011111            137             5F
96        1100000            140             60
97        1100001            141             61
98        1100010            142             62
99        1100011            143             63
100        1100100            144             64
101        1100101            145             65
102        1100110            146             66
103        1100111            147             67
104        1101000            150             68
105        1101001            151             69
106        1101010            152             6A
107        1101011            153             6B
108        1101100            154             6C
109        1101101            155             6D
110        1101110            156             6E
111        1101111            157             6F
112        1110000            160             70
113        1110001            161             71
114        1110010            162             72
115        1110011            163             73
116        1110100            164             74
117        1110101            165             75
118        1110110            166             76
119        1110111            167             77
120        1111000            170             78
121        1111001            171             79
122        1111010            172             7A
123        1111011            173             7B
124        1111100            174             7C
125        1111101            175             7D
126        1111110            176             7E
127        1111111            177             7F
128       10000000            200             80
129       10000001            201             81
130       10000010            202             82
131       10000011            203             83
132       10000100            204             84
133       10000101            205             85
134       10000110            206             86
135       10000111            207             87
136       10001000            210             88
137       10001001            211             89
138       10001010            212             8A
139       10001011            213             8B
140       10001100            214             8C
141       10001101            215             8D
142       10001110            216             8E
143       10001111            217             8F
144       10010000            220             90
145       10010001            221             91
146       10010010            222             92
147       10010011            223             93
148       10010100            224             94
149       10010101            225             95
150       10010110            226             96
151       10010111            227             97
152       10011000            230             98
153       10011001            231             99
154       10011010            232             9A
155       10011011            233             9B
156       10011100            234             9C
157       10011101            235             9D
158       10011110            236             9E
159       10011111            237             9F
160       10100000            240             A0
161       10100001            241             A1
162       10100010            242             A2
163       10100011            243             A3
164       10100100            244             A4
165       10100101            245             A5
166       10100110            246             A6
167       10100111            247             A7
168       10101000            250             A8
169       10101001            251             A9
170       10101010            252             AA
171       10101011            253             AB
172       10101100            254             AC
173       10101101            255             AD
174       10101110            256             AE
175       10101111            257             AF
176       10110000            260             B0
177       10110001            261             B1
178       10110010            262             B2
179       10110011            263             B3
180       10110100            264             B4
181       10110101            265             B5
182       10110110            266             B6
183       10110111            267             B7
184       10111000            270             B8
185       10111001            271             B9
186       10111010            272             BA
187       10111011            273             BB
188       10111100            274             BC
189       10111101            275             BD
190       10111110            276             BE
191       10111111            277             BF
192       11000000            300             C0
193       11000001            301             C1
194       11000010            302             C2
195       11000011            303             C3
196       11000100            304             C4
197       11000101            305             C5
198       11000110            306             C6
199       11000111            307             C7
200       11001000            310             C8
201       11001001            311             C9
202       11001010            312             CA
203       11001011            313             CB
204       11001100            314             CC
205       11001101            315             CD
206       11001110            316             CE
207       11001111            317             CF
208       11010000            320             D0
209       11010001            321             D1
210       11010010            322             D2
211       11010011            323             D3
212       11010100            324             D4
213       11010101            325             D5
214       11010110            326             D6
215       11010111            327             D7
216       11011000            330             D8
217       11011001            331             D9
218       11011010            332             DA
219       11011011            333             DB
220       11011100            334             DC
221       11011101            335             DD
222       11011110            336             DE
223       11011111            337             DF
224       11100000            340             E0
225       11100001            341             E1
226       11100010            342             E2
227       11100011            343             E3
228       11100100            344             E4
229       11100101            345             E5
230       11100110            346             E6
231       11100111            347             E7
232       11101000            350             E8
233       11101001            351             E9
234       11101010            352             EA
235       11101011            353             EB
236       11101100            354             EC
237       11101101            355             ED
238       11101110            356             EE
239       11101111            357             EF
240       11110000            360             F0
241       11110001            361             F1
242       11110010            362             F2
243       11110011            363             F3
244       11110100            364             F4
245       11110101            365             F5
246       11110110            366             F6
247       11110111            367             F7
248       11111000            370             F8
249       11111001            371             F9
250       11111010            372             FA
251       11111011            373             FB
252       11111100            374             FC
253       11111101            375             FD
254       11111110            376             FE
255       11111111            377             FF
256      100000000            400            100

Solution

#include <iostream>
#include <iomanip>

using namespace std;

int toBin ( int number );
int toOct ( int number );
void toHex(int number, char *hex);
inline char convertHexNotation(int number);

int main() {
  int i  = 1;
  char hex[3];
  cout <<  "Show decimal equivalent 1-256" << endl;
  cout << "Decimal" << setw(10) << "Binary" <<  setw(15) << "Octal" <<  setw(15) <<  "Hexadecimal" << endl;

  while (i <= 256) {
    toHex(i, hex);
    cout << i << setw(15) << toBin(i) << setw(15) << toOct(i) << setw(15) << hex << endl;
    i++;
  }
  return 0;
}

int toBin(int number) {
  int  mul = 1;
  int output = 0;
  while (number > 0) {
    output += number % 2 * mul;
    number /= 2;
    mul = mul * 10;
  }
  return output;
}

int toOct(int number) {
  int  mul = 1;
  int output = 0;
  while (number > 0) {
    output += number % 8 * mul;
    number /= 8;
    mul = mul * 10;
  }
  return output;
}

void toHex(int number, char *hex) {
  int  i = 0;
  int temp = number;
  int count = 0;
  int flag = 0;

  while (number > 0) {
    // add to the top of stack
    hex[0] = convertHexNotation(number % 16);

    if (number /= 16) {
      i  = count;
      // slide the stack for new items
      // a new number exists.
      // existing  stack |B|C|
      // stack size 2 count = 1
      // iterative slide from tail by 1.
      while (i >= 0) {
        hex[i+1] = hex[i];
        i--;
      }
    }
    count++;
  }
  // terminate the string
  hex[count] = '\0';
}

inline char convertHexNotation(int number) {
  int notation;

  switch(number) {
  case 10:
    notation = 'A';
    break;
  case 11:
    notation = 'B';
    break;
  case 12:
    notation = 'C';
    break;
  case 13:
    notation = 'D';
    break;
  case 14:
    notation = 'E';
    break;
  case 15:
    notation = 'F';
    break;
  default:
    notation = 48 + number;
    break;
  }
  return notation;
}

Sources