Thread: Star Pyramid

  1. #1
    Registered User subhadeepgayen's Avatar
    Join Date
    Jul 2010
    Posts
    9

    Lightbulb Star Pyramid

    Hi,

    I have successfully created a star pyramid using the below code, but i wanted to know if there is any other method by which i can do it using less loops or variables ??

    OR maybe how would you do the star pyramid yourself ?

    Code:
    SSSS*
    SSS***
    SS*****
    S*******
    
    S= space
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int height,i,j,k=0;
        printf("Enter the height of pyramid : ");
        scanf("%d",&height);
        
        for(i=1;i<=height;i++)
        {
            for(j=1;j<=height-i;j++)
            {
               printf(" ");
            } 
                                      
            for(j=1;j<=i+k;j++)
            {
               printf("*");
            }                 
            printf("\n");
            k+=1;
        }
        getch();
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Good job!

    I would add the needed return 0 to the end of main(), and replace getch() with the standard getchar(), unless you know it's only going to be used on compilers with conio.h (like Turbo C), which support getch().

    I prefer the k++ idiom, instead of k+=1, but that's not important.

    You've correctly identified the parts to the problem: the height, the preliminary spaces on each row, and the printing of the asterisks and newline.

  3. #3
    Registered User subhadeepgayen's Avatar
    Join Date
    Jul 2010
    Posts
    9
    Thanks for the suggestions

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing to code 5 point star function C++
    By YellowJello in forum C++ Programming
    Replies: 1
    Last Post: 01-18-2010, 11:54 PM
  2. Creating a half pyramid
    By rightbrainer in forum C Programming
    Replies: 7
    Last Post: 04-21-2009, 01:29 AM
  3. Number pyramid
    By Tehy in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 09:01 AM
  4. Star Wars or Star Trek
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 09-28-2001, 08:33 AM