I need to print some leading spaces and zeros before a number, so that the output will be like this:
00015
22
00111
8
126
here, I need to print leading spaces
when the number is even
and leading zero
when odd
Here's how I did it :
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<width ; i++)
printf(" ");
printf("%d\n",x);
}
else // number odd
{
digit=log10(x)+1; // number of digit in the number
for(i=digit ; i<width ; i++)
printf("0");
printf("%d\n",x);
}
Is there any shortcut way to do this ?