title: Printing leading spaces and zeros in C/C++
body: <p>I need to print some leading spaces and zeros before a number, so that the output will be like this:</p>
  
  <pre><code>00015
     22
  00111
      8
    126
  </code></pre>
  
  <p>here, I need to print <code>leading spaces</code> when the number is <code>even</code> and <code>leading zero</code> when <code>odd</code></p>
  
  <p>Here's how I did it :</p>
  
  <pre><code>int i, digit, width=5, x=15;
  
  if(x%2==0)  // number even
  {
      digit=log10(x)+1;  // number of digit in the number
      for(i=digit ; i&lt;width ; i++)
        printf(" ");
      printf("%d\n",x);
  }
  else       // number odd
  {
      digit=log10(x)+1;  // number of digit in the number
      for(i=digit ; i&lt;width ; i++)
        printf("0");
      printf("%d\n",x);
  }
  </code></pre>
  
  <p>Is there any shortcut way to do this ?</p>