Question:

How do I create this pyramid in C using loops?

by  |  earlier

1 LIKES UnLike

---*---

--* *--

-*-*-*-

*-*-*-*

The dashes represent spaces & should be blank space instead. I had to put them to properly format the question. The pyramid should be formatted exactly as above. i.e. spaces for dashes b/w 2 asterisk signs & margins.

Thanks.

 Tags:

   Report

2 ANSWERS


  1. Well, this should work:

    #define WIDTH 8

    int main() {

      int i, j;

      for (i = 0; i < WIDTH/2; i++) {

        for (j = 0; j < WIDTH/2 - i; j++) printf(" ")

        for (j = 0; j < i; j++) printf("* ");

        printf("\n");

      }

      return 0;

    }

    Hope that works!


  2. include<stdio.h>

    #include<conio.h>

    #define SPACE' '

    #define MID 40

    int main()

    {

    int i,line,n;

    clrscr();

    printf("\nEnter No. of line::");

    scanf("%d",&n);

    for(line=1;line<=n;line++)

    {

    printf("\n");

    for(i=1;i<=MID-line;i++)          //loop for space

    printf("%c",SPACE);

    for(i=1;i<=line;i++)              //loop for accending

    printf("*");

    for(i=line-1;i>=1;i--)              //loop  for descending

    printf("*");

    }

    getch();

    return 0;

    }

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.