title: Decimal to hexadecimal conversion code in C++
body: <p>I'm a complete newbie to C++ programming. I've been given the task to code decimal to hexadecimal conversion. This is what I've done so far.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int main()
  {
      long dec;
      int rem;
  
      std::cout &lt;&lt; "enter decimal number: ";
      std::cin &gt;&gt; dec;
  
      while (dec &gt; 0) {
          rem = dec % 16;
          if (rem &gt; 9) {
              switch (rem) {
                 case 10: std::cout &lt;&lt; "A"; break;
                 case 11: std::cout &lt;&lt; "B"; break;
                 case 12: std::cout &lt;&lt; "C"; break;
                 case 13: std::cout &lt;&lt; "D"; break;
                 case 14: std::cout &lt;&lt; "E"; break;
                 case 15: std::cout &lt;&lt; "F"; break;
              }
          }
          else {
              std::cout &lt;&lt; rem;
          }
          dec = dec / 16;
      }
  }
  </code></pre>
  
  <p>It gives answer backwards, like if I input a decimal number '650' it gives hexadecimal value <code>A82</code> when the answer should be <code>28A</code>. Can anyone suggest what I should do to correct it?</p>