Thread: Number pyramid

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Finland
    Posts
    16

    Number pyramid

    I just coded number pyramid:

    Code:
    #include <stdio.h>
    
    int main (int argc, const char * argv[]) {
     int i, j, k, l;
     int number = 0;
     l = 1;
    
    	for (i = 0; i < 10; i++) {
    	
    		for (j = 10; j > i; j--) 
    			printf(" ");
    			
    		for (k = 0; k < l; k++) 
    			printf("%d", number);
    	
    	
    	printf("\n");
    	l += 2;
    	number += 1;
    	
    	}
    
        return 0;
    }
    Code works great and the outputs number pyramid(Looks like the forum doesn't show it right, but I'm sure you know what pyramid is ):
    0
    1 1 1
    2 2 2 2 2
    3 3 3 3 3 3 3
    4 4 4 4 4 4 4 4 4
    5 5 5 5 5 5 5 5 5 5 5
    6 6 6 6 6 6 6 6 6 6 6 6 6
    7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
    8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
    9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9

    But I want the output be like this (Looks like the forum doesn't show it right, but I'm sure you know what pyramid is ):

    1
    2 3 2
    3 4 5 4 3
    4 5 6 7 6 5 4
    5 6 7 8 9 8 7 6 5
    6 7 8 9 0 1 0 9 8 7 6
    7 8 9 0 1 2 3 2 1 0 9 8 7
    8 9 0 1 2 3 4 5 4 3 2 1 0 9 8
    9 0 1 2 3 4 5 6 7 6 5 4 3 2 1 0 9
    0 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 0


    I just cant figure it out how to do it. Any ideas?

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    16
    Not much help, but =>


    1
    2 3 2
    3 4 5 4 3
    4 5 6 7 6 5 4
    5 6 7 8 9 8 7 6 5
    6 7 8 9 0 1 0 9 8 7 6
    7 8 9 0 1 2 3 2 1 0 9 8 7
    8 9 0 1 2 3 4 5 4 3 2 1 0 9 8
    9 0 1 2 3 4 5 6 7 6 5 4 3 2 1 0 9
    0 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 0
    ^-^

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    Code:
    	int x,y;
    	int rows = 10;
    	for (y = 1; y <= rows; ++y) {
    		for (x = y; x < rows; ++x) {
    			printf(" ");
    		}
    		for (x = 0; x < y; ++x) {
    			printf("&#37;d",(y+x) % 10);
    		}
    		for (x = y - 1; x > 0; --x) {
    			printf("%d",(y+x-1) % 10);
    		}
    		printf("\n");
    	}

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Heres my solution, although it may be unnecessarily complicated, it should work:
    Code:
    #include <stdio.h>
    
    int main()
    {
         int x, y, z=0;
         for(y=1; y<=10; y++)
         {
             for(x=1; x<(11-y); x++) printf(" ");
             for(x=1; x<(y*2); x++)
             {
                 if(x <= y)
                     if(y+(x-1) > 9) printf("%i", y+(x-11));  
                     else            printf("%i", y+(x-1));
                 else      
                     if((y*2+z-x) > 9) printf("%i", (y*2+z)-x-10);
                     else              printf("%i", (y*2+z)-x);    
             }
             z++;
             printf("\n");         
         }            
         getchar(), getchar();
         return 0;
    }

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Here's a solution that actually gets the spaces between the numbers:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int num_to_show;
      int line_start_num;
      int i, j;
    
      for(i = 1;i <= 10;++i)
      {
        num_to_show = i * 2 - 1;
    
        // Print leading spaces
        for(j = 0;j < 20 - num_to_show;++j)
          putchar(' ');
    
        // Count up for half a line
        line_start_num = num_to_show - num_to_show / 2;
        for(j = 0;j < num_to_show / 2 + 1;++j)
          printf("&#37;d ", line_start_num++ % 10);
    
        // And then count down for half a line
        line_start_num--;
        for(j = 0;j < num_to_show / 2;++j)
          printf("%d ", --line_start_num % 10);
        putchar('\n');
      }
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    If you want spaces between the numbers, simply put a space after the &#37;d on lines 8 and 11 of my code, and double the number of spaces printed prior to the row (line 5):

    Code:
    	int x,y;
    	int rows = 10;
    	for (y = 1; y <= rows; ++y) {
    		for (x = y; x < rows; ++x) {
    			printf("  ");
    		}
    		for (x = 0; x < y; ++x) {
    			printf("%d ",(y+x) % 10);
    		}
    		for (x = y - 1; x > 0; --x) {
    			printf("%d ",(y+x-1) % 10);
    		}
    		printf("\n");
    	}
    I still have the shortest code
    I could probably make it even shorter if I used the abs() function.

    Code:
    	int x,y;
    	int rows = 10;
    	for (y = 1; y <= rows; ++y) {
    		for (x = y; x < rows; ++x) {
    			printf("  ");
    		}
    		for (x = 1; x < y * 2; ++x) {
    			printf("%d ",(y + y - abs(x-y) - 1) % 10);
    		}
    		printf("\n");
    	}
    Last edited by IsmAvatar2; 05-30-2007 at 12:29 PM.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    How about this?

    Code:
    void pyr(int start, int size)
    {
       int i;
    
       for(i = 0; i < size; i++)
       {
          printf("%d ", start % 10);
          start += (i < size / 2) ? 1 : -1;
       }
       putchar('\n');
    }
    
    int main()
    {
       int i;
       int max = 10;
    
       for(i = 1; i <= max; i++)
       {
          printf("%*s", 2 * (max - i), "");
          pyr(i, 2 * i - 1);
       }
    }

  8. #8
    Registered User
    Join Date
    Apr 2007
    Location
    Finland
    Posts
    16
    Thanks a lot! I will try these out!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM