954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

My loop is not working properly

void printer(char x)
{
    int d, b, z;

    for(z=x; z>=1; z--) {
        for(b=z; b<=x-1; b++)
            printf(" ");
        for(d=z; d>=1; d--)
            printf("%d ", d);
        printf("\n");
    }
}

that's my code which is supposed to print
4 3 2 1
4 3 2
4 3
4
when input is 4. however, it prints as
4 3 2 1
3 2 1
2 1
1

ineedsomehelp:3
Newbie Poster
24 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

Change this

for(d=z; d>=1; d--)

to this

for(d=x; d>x-z; d--)

Now tell me why that works and what you were doing wrong. :)

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

You could use up counting for to increment the stop condition of inner loop and manage with two nested fors.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

In the line 1 declare x as int instead of char. It would be better that way.

Arbus
Practically a Master Poster
615 posts since Dec 2010
Reputation Points: 45
Solved Threads: 31
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: