Thread: Printing a reverse pyramid using C

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    6

    Unhappy Printing a reverse pyramid using C

    Hello fellow programmers,
    I'm a beginner in learning C and stuck at a problem.
    As the title suggests, I want to print a Reverse pyramid. Now it was easy if I wanted to print asterisk(*) and I successfully (with the help of an online tutorial) fulfilled this task by applying the following code:

    Code:
    int main (void){
        int i,n,j;
        n=6;
        for(i=n;i>0;i--)
        {
            for(j=n-i;j>0;j--)
                printf("  ");
            for(j=2*i-1;j>0;j--)
                printf(" *");
            printf("\n");
        }
        return 0;
    }
    But the problem arises now when I want integers instead of * in the output. And the requirement is that they be in following particular order:

    Code:
    Output:
       1111111111
        22222222
         333333
          4444
           55
    I tried many times manipulating the declarations and definitions and for loops but all in vain. Any help in this regard would be much appreciated.

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Well, you've already done the hard work and all you need to do is use a variable that changes by a value of one, with each row.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    6
    Quote Originally Posted by gemera View Post
    Well, you've already done the hard work and all you need to do is use a variable that changes by a value of one, with each row.
    Thanks for the quick reply. A little bit elaboration would be required I'm afraid.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Ok, the easier way to begin with would be to number from 6 at the base down to 1 at the tip. How would you do that?

  5. #5
    Registered User
    Join Date
    Apr 2015
    Posts
    6
    Instead of * I will use this statement to print for 6 to 1 at the tip.
    Code:
    printf(" %d",i);

  6. #6
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Yup, and a little bit of arithmetic will now get you what you wanted originally.

  7. #7
    Registered User
    Join Date
    Apr 2015
    Posts
    6
    I'm trying..Its been 2 hours manipulating digits here and there, trying different arithmetic operations. I just can't get to it.
    Can you tell me exactly how would I print that target? Please?

  8. #8
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    This site has a policy of not just giving out solutions, and I wouldn't be helping you long term by doing so.

    An easier problem to try and solve from where you are now would be to try and number from 0 at the base instead of 1.

    If you have that - moving to the solution you want should be trivial.

  9. #9
    Registered User
    Join Date
    Apr 2015
    Posts
    6
    I just read the rules. I apologize for asking for the solution.
    I'll try and post the results once I'm done. Thanks for the help.

  10. #10
    Registered User
    Join Date
    Apr 2015
    Posts
    6
    Done. Its working. Thanks for the help and hints.

    Code:
    int main (void){
        int i,n,j;
        n=5;
        for(i=1;i<=n;i++)
        {
    		for(j=1;j<=i-1;j++)
                printf("  ");
            for(j=1;j<=12-2*i;j++)
                printf(" %d",i);
            printf("\n");
        }
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Digits pyramid printing programs needed in C Programming
    By technoexam in forum C Programming
    Replies: 8
    Last Post: 10-01-2011, 04:58 PM
  2. Reverse order printing
    By JFonseka in forum C Programming
    Replies: 1
    Last Post: 08-19-2007, 06:47 AM
  3. Printing in reverse order
    By JFonseka in forum C Programming
    Replies: 1
    Last Post: 08-17-2007, 04:30 AM
  4. Printing my name in reverse
    By Guti14 in forum C++ Programming
    Replies: 8
    Last Post: 08-11-2003, 02:15 PM