-
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();
}
-
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.
-
Thanks for the suggestions :)