# book exercise

This is a discussion on book exercise within the C# Programming forums, part of the General Programming Boards category; the following exercise is from a book im learning from... Write two programs that each a table of the binary, ...

1. ## book exercise

the following exercise is from a book im learning from...

Write two programs that each a table of the binary, octal, and hexadecimal equivalents of the decimal numbers in the range of 1-256

a) for the first program, print the results to the console without using any string formats.

b) for the second program, print the results to the console using both the deciamal and hexadecimal string formats (there are no formats for binary and octal in C#).

i have completed the octal conversion for the first program ...

Code:
```using System;

class Test
{

static void Main(string[] args)
{
int octal = 1;

Console.WriteLine("Decimal\t\tOctal");

for(int dec=1; dec<=256; dec++, octal++)
{
Console.Write(dec + "\t\t");//decimal

if(dec%8==0)
octal += 2;

switch(dec)
{
case 64: octal = 100;
break;

case 128: octal = 200;
break;

case 192: octal = 300;
break;

case 256: octal = 400;
break;

}//end switch

Console.Write(octal);
Console.WriteLine();

}//end loop

Console.WriteLine("Voila!");
}

}```
this works, i guess the code could be more efficient, with regards to the binary and hex conversions i was hoping to get some help with the algorithm or at least point me in the right direction please

luigi

2. ## more efficient code

i have reduced the code as follows for the octal conversion

Code:
```using System;

class Test
{

static void Main(string[] args)
{
int octal = 1; int octtemp = 100;

Console.WriteLine("Decimal\t\tOctal");

for(int dec=1; dec<=256; dec++, octal++)
{
Console.Write(dec + "\t\t");//decimal

if(dec%8==0 && dec%64!=0)
octal += 2;

else
if(dec%64 == 0)
{
octal = octtemp;
octtemp += 100;
}

Console.Write(octal);
Console.WriteLine();

}//end loop

Console.WriteLine("Voila!");
}

}```
now going to work out hex algo .. hmmm how am i going to produce the letters A-F ?

luigi