how to display

How can I make an output like this:
54321
_4321
__321
___21
____1

__ are spaces

I have started this codes.. but can't make it the output like that.

for(inti=1;i<=num;i++)
{for(intj=i;j<=i-1;j--)
for(intk=num;k>=1;k--)
{ printf("%d",k);
printf("\n");
}
}


thanks
Are you using C or C++ ?
C++ has stream manipulators for these things: http://www.cplusplus.com/reference/iostream/manipulators/
For C use this: printf ( "%5d\n", number ); where the '5' is the number of digits to fill
Last edited on
I'm using C

--am I on the right forum? topic?

-- sorry if i'm lost.. thanks.
-- I was editing my post, added the C version --
Thanks..

If I'm to enter any number, is my codes correct?

may I know t purpose of printf("%5d\n") ?

I was able to produce this display but this is not what i need to display

54321
54321
54321
54321
54321


this is my code:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,j,num;
clrscr();
printf("Enter any number:\t");
scanf("%d",&n);

for(i=1;i<=num;i++)
{
for(j=1;j<=1-i;j--)
for(k=num;k>=1;k--)
{
printf("%d",k);
printf("\n");
}
}
}
getch();
}
Last edited on
That would display 54321 if number == 54321, _4321 if number == 4321 etc.
If this could help you 54321 % 10000 = 4321
hmm.. i can't understand it.
did I miss something on my code?
You should evaluate the number (removing the 1st digit) and then display it.
A simpler way may be converting the number into a string and then replace the leading characters with a space
ok, I'll try. Thanks.
does it require 3 for loops?
I think just one loop would be enough
I can't really make it.
I think i'll quit for now and try again tomorrow.

Thanks.
Your problem is solved easily by using a function. I did this function years ago in Pascal. I'll see if I can remember

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
typedef enum _TextAlignmentEnum
{
    LeftAlignment,
    RightAlignment,
    CenterAlignment
} TextAlignmentEnum;

//I'm a Windows guy.  I never write char *.  I write LPSTR.  LPCSTR means const char *.

LPSTR AlignText(LPCSTR szText, int columnSize, TextAlignmentEnum alignment)
{
    LPSTR szResult;
    if (strlen(szText) >= columnSize)
    {
        //Nothing to do.  Return a copy of the original string.
        szResult = new char[strlen(szText) + 1];
        strcpy(szResult, szText); //Or maybe the arguments are backwards.   Double check this.
        return szResult;
    }

    int whiteSpaceCount = columnSize - strlen(szText);
    int leftWSCount;
    switch (alignment)
    {
        case LeftAlignment:
            //I leave this one for you to complete as exercise.
            break;
        case RightAlignment:
            //Your question.  I'll answer.
            leftWSCount = whiteSpaceCount;
            break;
        case CenterAlignment:
            //Also an exercise for you.
            break;
    }

    szResult = new char[columnSize + 1];
    //I don't remember if there is an easier way to initialize the string.
    for(int i = 0; i < columnSize; i++)
    {
        szResult[i] = ' ';
    }

    strcpy(&szResult[leftWSCount], szText);
    return szResult;
}


The above function should return the specified text in the szText parameter, but padded to the left or right (or both) to achive the desired result within the specified imaginary (or not-so-imaginary) column size.

Example:
1
2
3
4
5
6
LPSTR szFormattedText = AlignText("This right-aligned text!", 50, RightAlignment);
cout << szFormattedText;
delete[] szFormattedText;
szFormattedText = AlignText("Some more right-aligned text.  Larger, though.", 50, RightAlignment);
cout << szFormattedText;
delete[] szFormattedText;


Always make sure you delete[] the string returned by AlignText. If you don't, you'll be leaking memory.
If you are using cout, is much easier using manipulators than all that....
Dunno about that Bazzy. In real life, I only program C++ components, not entire applications, and much less console applications. My knowledge of C++ is 100% used to create Windows DLL's, either standard or ActiveX. :-)

Feel free to enlight me (and all of us) with an optimized version. The only consoles I ever did were the examples in "Programming Windows with C++" by Charles Petzold, and the guy uses printf().
With manipulators is really easy:
cout << left << setfill('.') << setw(16) << "foobar";
In this line you give the alignment, the character which will fill the empty spaces, the length you want in a single line. After that, it will work with any type, not only for C strings
Cool indeed. I had absolutely no idea. :-P

Will the output be the one below?


foobar..........


And in order to solve the right alignment, do I change cout << left to cout << right?
Yes, if you want to see more other of these manipulators see http://www.cplusplus.com/reference/iostream/manipulators/ they can do whatever the printf format allows ( and some more things )
Last edited on
these manipulators would have done the job, but imho author forgot to tell us that this is his home assignment no. 3 (loops) - but that's only my guess :-)
something like here: http://www.cplusplus.com/forum/beginner/12597/
@webJose
Thanks, but I can't understand the code.. Sorry, I'm just new in programming.

@Bazzy
Thanks for the usual help.

@johnkravetzki
yup! It was an assignment given. I am required to use 3 loops using C.

Thanks guys, I finally made it.

for(i=1;i,=num;i++)
{
for(j=1;j<=i-1;j++)
printf("");
for(k=num-i+1;k>=1;k--)
{
printf("%d",k);
}
printf("\n");
}


^_^
Topic archived. No new replies allowed.