printf() function

B

babyip

hello, i have a problem using printf() function, would u please help me?

i have a program to print numbers on screen, each number occupies five
spaces and the range of the number is from -9 to 9.
my question is, how can i place the number in the middle of the five spaces?
thanks!

here is my example (allow me to use underline "_" to represent space " ")

"__1__" <==positive number at the third position
"__-4_" <==the negative symbol at the third position, and the number at
the fourth position
 
J

Joe Wright

babyip said:
hello, i have a problem using printf() function, would u please help me?

i have a program to print numbers on screen, each number occupies five
spaces and the range of the number is from -9 to 9.
my question is, how can i place the number in the middle of the five spaces?
thanks!

here is my example (allow me to use underline "_" to represent space " ")

"__1__" <==positive number at the third position
"__-4_" <==the negative symbol at the third position, and the number at
the fourth position
#include <stdio.h>
int main(void) {
int i;
for (i = -9; i < 10; ++i)
printf(" %d%s\n", i, i < 0 ? " " : " ");
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top