This function prints every number starting from 0000 to 1000.
#include <stdio.h>
int main() {
int i = 0;
while ( i <= 1000 ) {
printf( "%04d\n", i);
i = i + 1;
}
return 0;
}
The output looks like this:
0000
0001
0002
etc..
How can I make this more presentable using three different columns (perhaps using \t ) to have the output look like this:
0000 0001 0002
0003 0004 0005
etc..
This is how I'd do it, to eliminate the potentially expensive if/?: statement:
char nl[3] = {' ', ' ', '\n'};
while ( i <= 1000 ) {
printf("%04d%c", i, nl[i%3]);
++i;
}
Note that the modulo division may be much more expensive than the branching anyhow, but this depends on your architecture.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With