Results 1 to 7 of 7
  1. #1
    Kareem Mesbah is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Default table of numbers

    hi, I am asked to make an application that displays table of decimal numbers in range of 1 to 256 the program should display the decimal number binary, octal and hexadecimal equivalents.

    my problem is with the hexadecimal equivalents, I can handle the decimal numbers from 10 to 15 to be displayed as letters from A to F
    but I cannot handle them while they're part of hexadecimal number. for example I can not display number like this "4D3B"

    here's my code, I'll be thankful if there are some hints.

    Java Code:
    public class Table {
    	
    	public static void main(String[] args) {
    		
    		
    		int decimal, reminder, position, binary;
    		
    		System.out.printf ("%-20s\t\t%-20s\t\t%-20s\t\t%-20s\n","Decimal","Binary","Octal","Hexadecimal");
    		
    		for (int i = 1; i <= 256; i++) {
    			
    			decimal = i;
    			position = 1;
    			binary = 0;
    			
    			while (decimal > 0) {
    				
    				reminder = decimal % 2;
    				decimal/= 2;
    				binary += (reminder * position);
    				position*=10;
    				
    			}// end inner while
    			
    			
    			System.out.printf("%-20d\t\t%-20d\t\t", i,binary);
    			
    			decimal = i;
    			position = 1;
    			int octal = 0;
    			while (decimal > 0) {
    				reminder = decimal % 8;
    				decimal /= 8;
    				octal += reminder*position;
    				position *= 10;
    			}
    			
    			System.out.printf("%-20d\t\t",octal);
    			
    			decimal = i;
    			position = 1;
    			int hexadecimal = 0;
    			
    			while (decimal > 0) {
    				reminder = decimal % 16;
    				decimal /= 16;
    				hexadecimal += reminder * position;
    				position *= 10;
    			}
    			
    			if (i >= 10 && i <= 15) {
    				String converter = "";
    				
    				switch (i) {
    				case 10:converter = "A";
    					break;
    				case 11:converter = "B";
    					break;
    				case 12:converter = "C";
    					break;
    				case 13:converter = "D";
    					break;
    				case 14:converter = "E";
    					break;
    				case 15:converter = "F";
    					break;
    				}// end switch
    				
    				
    			System.out.printf("%-20s\n",converter);
    			}// end if
    			
    			else {
    			
    			System.out.printf("%-20d\n", hexadecimal);
    			
    			}
    		}// end for
    	
    		
    	}// end main
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: table of numbers

    I can not display number like this "4D3B"
    How do you get that large number? 256 = 0x100

    Can you post the program's output and explain what is wrong with it?

    Another way to convert int values to String or char would be to use an array of chars: '0' to 'F'. Use the int value as index to the array.
    Last edited by Norm; 10-03-2012 at 12:57 AM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Kareem Mesbah is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Default Re: table of numbers

    actually I am not asked to use arrays, I am sorry about the random number I wrote. this is the program output

    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 20
    27 11011 33 21
    28 11100 34 22
    29 11101 35 23
    30 11110 36 24
    31 11111 37 25
    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 30
    43 101011 53 31
    44 101100 54 32
    45 101101 55 33
    46 101110 56 34
    47 101111 57 35
    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 40
    59 111011 73 41
    60 111100 74 42
    61 111101 75 43
    62 111110 76 44
    63 111111 77 45
    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 50
    75 1001011 113 51
    76 1001100 114 52
    77 1001101 115 53
    78 1001110 116 54
    79 1001111 117 55
    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 60
    91 1011011 133 61
    92 1011100 134 62
    93 1011101 135 63
    94 1011110 136 64
    95 1011111 137 65
    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 70
    107 1101011 153 71
    108 1101100 154 72
    109 1101101 155 73
    110 1101110 156 74
    111 1101111 157 75
    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 80
    123 1111011 173 81
    124 1111100 174 82
    125 1111101 175 83
    126 1111110 176 84
    127 1111111 177 85
    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 90
    139 10001011 213 91
    140 10001100 214 92
    141 10001101 215 93
    142 10001110 216 94
    143 10001111 217 95
    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 100
    155 10011011 233 101
    156 10011100 234 102
    157 10011101 235 103
    158 10011110 236 104
    159 10011111 237 105
    160 10100000 240 100
    161 10100001 241 101
    162 10100010 242 102
    163 10100011 243 103
    164 10100100 244 104
    165 10100101 245 105
    166 10100110 246 106
    167 10100111 247 107
    168 10101000 250 108
    169 10101001 251 109
    170 10101010 252 110
    171 10101011 253 111
    172 10101100 254 112
    173 10101101 255 113
    174 10101110 256 114
    175 10101111 257 115
    176 10110000 260 110
    177 10110001 261 111
    178 10110010 262 112
    179 10110011 263 113
    180 10110100 264 114
    181 10110101 265 115
    182 10110110 266 116
    183 10110111 267 117
    184 10111000 270 118
    185 10111001 271 119
    186 10111010 272 120
    187 10111011 273 121
    188 10111100 274 122
    189 10111101 275 123
    190 10111110 276 124
    191 10111111 277 125
    192 11000000 300 120
    193 11000001 301 121
    194 11000010 302 122
    195 11000011 303 123
    196 11000100 304 124
    197 11000101 305 125
    198 11000110 306 126
    199 11000111 307 127
    200 11001000 310 128
    201 11001001 311 129
    202 11001010 312 130
    203 11001011 313 131
    204 11001100 314 132
    205 11001101 315 133
    206 11001110 316 134
    207 11001111 317 135
    208 11010000 320 130
    209 11010001 321 131
    210 11010010 322 132
    211 11010011 323 133
    212 11010100 324 134
    213 11010101 325 135
    214 11010110 326 136
    215 11010111 327 137
    216 11011000 330 138
    217 11011001 331 139
    218 11011010 332 140
    219 11011011 333 141
    220 11011100 334 142
    221 11011101 335 143
    222 11011110 336 144
    223 11011111 337 145
    224 11100000 340 140
    225 11100001 341 141
    226 11100010 342 142
    227 11100011 343 143
    228 11100100 344 144
    229 11100101 345 145
    230 11100110 346 146
    231 11100111 347 147
    232 11101000 350 148
    233 11101001 351 149
    234 11101010 352 150
    235 11101011 353 151
    236 11101100 354 152
    237 11101101 355 153
    238 11101110 356 154
    239 11101111 357 155
    240 11110000 360 150
    241 11110001 361 151
    242 11110010 362 152
    243 11110011 363 153
    244 11110100 364 154
    245 11110101 365 155
    246 11110110 366 156
    247 11110111 367 157
    248 11111000 370 158
    249 11111001 371 159
    250 11111010 372 160
    251 11111011 373 161
    252 11111100 374 162
    253 11111101 375 163
    254 11111110 376 164
    255 11111111 377 165
    256 100000000 400 100


    as you can see there are many wrong numbers in the hexadecimal column such as 165,164 and so on.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: table of numbers

    Looks like the hex column goes wrong at 26. The code generates 20 vs 1A

    Take that one number: 26 and work through you code to see why it outputs 20 vs 1A
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Kareem Mesbah is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

    Default Re: table of numbers

    I tried to use the toString(); method but for sorry I got the number reversed
    here's my bit of code and part of the program output

    Java Code:
    while (decimal > 0) {
    				reminder = decimal % 16;
    				decimal /= 16;
    				position *= 10;
    				if (reminder >= 10 && reminder <= 15) {
    					switch (reminder) {
    					case 10: conv = "A";
    						break;
    					case 11: conv = "B";
    						break;
    					case 12: conv = "C";
    						break;
    					case 13: conv = "D";
    						break;
    					case 14: conv = "E";
    						break;
    					case 15: conv = "F";
    						break;
    					}// end switch
    				}// end if
    				else {
    					conv = Integer.toString(reminder);
    				}
    				hexa += conv;
    			}// end while
    Decimal Binary Octal Hexadecimal
    16 10000 20 01
    17 10001 21 11
    18 10010 22 21
    19 10011 23 31
    20 10100 24 41
    21 10101 25 51
    22 10110 26 61
    23 10111 27 71
    24 11000 30 81
    25 11001 31 91
    26 11010 32 A1

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: table of numbers

    I got the number reversed
    Don't use the shortcut +=
    Write a full statement to concatenate the digits in the desired order.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Kareem Mesbah is offline Member
    Join Date
    Sep 2012
    Posts
    15
    Rep Power
    0

Similar Threads

  1. Replies: 3
    Last Post: 02-23-2012, 09:30 AM
  2. Replies: 1
    Last Post: 09-27-2011, 06:06 AM
  3. Replies: 11
    Last Post: 01-14-2011, 06:36 PM
  4. Replies: 1
    Last Post: 01-08-2010, 06:19 AM
  5. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •