how to print the output from right to left

example:
1
2
3
4
printf("Result");
printf("123\n");
printf("1234\n");
printf("12345\n");


the result will be
Result
123
1234
12345


If i want to make the output to be like

 123
 1234
12345



Can anyone guide me?
Last edited on
Yes.
printf("$20s", "a string");
            a string

It's right-aligned with spaces if it isn't 20 or more chars long. You can use a - sign for a left-align.
Last edited on
:) okok.
Hmm...... just input the spaces before the numbers and align them the way you want.

1
2
3
4
5
printf("Result");
printf("     123\n");
printf("   1234\n");
printf(" 12345\n");

but if you stored the numbers in a variable(for e.g result) then use:

 
printf("Result:  %5d",result);


then you.ll have all outputs frm 'result' left-aligned... try it let me knw if ya still hve problem.
@ManuAizen,
1.
just input the spaces before the numbers and align them the way you want.

Yes, let's write specific code that can only be used in certain cases
2. I already said that. Also, you can use a temporary string or number for the variable.
3. It does not left align it. It right-aligns it. You have to put "%-5d" for a left-align.

I should have mentioned that this works for all the formats that printf() (actually, vsprintf()) accepts.
Last edited on
@ chrisname,

hmm, yes indeed, sorry i forgot but the ' - '. my mistake.
Topic archived. No new replies allowed.