title: Optimizing conversion algorithm
body: <p>I have recently been working on a exercise in a book I have been reading. The task was to create a program that prints all the numbers between 1-256 in their binary, octal and hexadecimal equivalents. We were only supposed to use methods we had learned so far in the book, which meant only using for, while and do..while loops, if and else if statements, converting integers to ASCII equivalents and some more basic stuff (e.g. cmath and iomanip). </p>
  
  <p>So after some work, here is my result. However, it is messy and un-elegant and obfuscated. Does anyone have any suggestions to increase code efficiency (or elegance... :P) and performance?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;cmath&gt;
  using namespace std;
  
  int main()
  {
  int decimalValue, binaryValue, octalValue, hexadecimalValue, numberOfDigits;
  cout &lt;&lt; "Decimal\t\tBinary\t\tOctal\t\tHexadecimal\n\n";
  for (int i = 1; i &lt;= 256; i++)
  {
      binaryValue = 0;
      octalValue = 0;
      hexadecimalValue = 0;
      if (i != 0) 
      {
      int x, j, e, c, r = i, tempBinary, powOfTwo, tempOctal, tempDecimal;
      for (j = 0; j &lt;=8; j++) //Starts to convert to binary equivalent
      {
          x = pow(2.0, j);
          if (x == i)
          {
                powOfTwo = 1;
                binaryValue = pow(10.0, j);
                break;
          }
          else if (x &gt; i)
          {
                powOfTwo = 0;
                x /= 2;
                break;
          }
      }
      if (powOfTwo == 0)
      {
      for (int k = j-1; k &gt;= 0; k--)
      {
          if ((r-x)&gt;=0)
          {
             r -= x;
             tempBinary = pow(10.0, k);
             x /= 2;
          }
          else if ((r-x)&lt;0)
          {
             tempBinary = 0;
             x /= 2;
          }
          binaryValue += tempBinary;
      }
      } //Finished converting
      int counter = ceil(log10(binaryValue+1)); //Starts on octal equivalent
      int iter;
      if (counter%3 == 0)
      {
         iter = counter/3;
      }
      else if (counter%3 != 0)
      {
         iter = (counter/3)+1; 
      }
      c = binaryValue;
      for (int h = 0; h &lt; iter; h++)
      {
          tempOctal = c%1000;
          int count = ceil(log10(tempOctal+1));
          tempDecimal = 0;
          for (int counterr = 0; counterr &lt; count; counterr++)
          {
              if (tempOctal%10 != 0)
              {
                   e = pow(2.0, counterr);
                   tempDecimal += e;
              }
              tempOctal /= 10;
          }
          octalValue += (tempDecimal * pow(10.0, h));
          c /= 1000;
      }//Finished Octal conversion
      cout &lt;&lt; i &lt;&lt; "\t\t" &lt;&lt; binaryValue &lt;&lt; setw(21-counter) &lt;&lt; octalValue &lt;&lt; "\t\t";
      int c1, tempHex, tempDecimal1, e1, powOf;
      char letter;
      if (counter%4 == 0)//Hexadecimal equivalent
      {
         iter = counter/4;
      }
      else if (counter%4 != 0)
      {
         iter = (counter/4)+1;
      }
      c1 = binaryValue;
      for (int h = 0, g = iter-1; h &lt; iter; h++, g--)
      {
          powOf = g*4;
          if (h == 0)
          {
                tempHex = c1 / pow(10.0, powOf);
          }
          else if (h &gt; 0)
          {
               tempHex = c1 / pow(10.0, powOf);
               tempHex %= 10000;
          }
          int count = ceil(log10(tempHex+1));
          tempDecimal1 = 0;
          for (int counterr = 0; counterr &lt; count; counterr++)
          {
              if (tempHex%10 != 0)
              {
                   e1 = pow(2.0, counterr);
                   tempDecimal1 += e1;
              }
              tempHex /= 10;
          }
          if (tempDecimal1 &lt;= 9)
          {
          cout &lt;&lt; tempDecimal1;
          }
          else if (tempDecimal1 &gt; 9)
          {
          cout &lt;&lt; char(tempDecimal1+55); //ASCII's numerical value for A is 65. Since 10-15 are supposed to be letters you just add 55
          }
      }
      cout &lt;&lt; endl;
      }
  }
  system("pause");
  return 0;
  }
  </code></pre>
  
  <p>Any recommendations for improvement will be appreciated.</p>