There is an alternative as short as Thiago's answer, but it works with numbers that are not multiples of 4:
int main(){
unsigned short int in, i;
scanf("%hd", &in);
for( i = 1; i <= in; i++ ) printf( i & 3 ? "%d " : "PUM\n", i );
return 0;
}
See working with the number 10 as an example in IDEONE .
The i & 3
operation is a quick way to get the 2 least significant bits of the counter, effectively returning 0
for all cases where the "PUM" message should be displayed.
/ li>
-
The ternary operator will use "%d "
in all cases where the mentioned expression does not return 0
- I have shown the ternary as an alternative to
if
, but it is worth saying that normally, if the criterion is efficiency, the structure similar to the @bigown response is more appropriate, though longer. / li>
Follow the code similar to @bigown by changing the rest operator by bits operation:
int main(){
int in, i;
scanf("%d", &in);
for ( i = 1; i <= in; i++ ) {
if (i & 3) {
printf("%d ", i);
} else {
printf("PUM\n");
}
}
return 0;
}
I've put a demo in IDEONE with the same logic but in less lines.