title: Decimal to octal in C
body: <p>I have just begun teaching myself C out of K.N King's C Programming: A Modern Approach (2ndEdn). </p>
  
  <p>I'm enjoying it, but am hoping to post the odd question here for advice if appropriate because unfortunately I don't have a tutor and some bits raise more questions then they answer!</p>
  
  <p>I'm doing a question on taking an integer entered and displaying it in octal. It says there is an easy way to do it, but that comes later in the book. I have come up with the following:</p>
  
  <pre><code>// Convert a number to octal
  
  int n, n2, n3, n4, n5, n6;
  
  printf("Enter a number between 0 and 32767: ");
  
  scanf("%d", &amp;n);
  
  n6 = n % 8;
  n5 = (n / 8) % 8;
  n4 = ((n / 8) / 8) % 8;
  n3 = (((n / 8) / 8) / 8) % 8;
  n2 = ((((n / 8) / 8) / 8) / 8) % 8;
  
  printf("%d%d%d%d%d", n2, n3, n4, n5, n6);
  </code></pre>
  
  <p>It works OK, but I'm not good at math and was wondering if there is a more efficient way of doing this or have I done it the only way possible...</p>
  
  <p>If anyone else has the book it's Q4 p.71.</p>
  
  <p>Thanks for your time.
  Andrew</p>
  
  <p>P.S I did look in the search engine but couldn't find anything that was doing it this 'slower' way!</p>