Home  >  Article  >  Backend Development  >  C program to print hollow pyramid and rhombus patterns

C program to print hollow pyramid and rhombus patterns

PHPz
PHPzforward
2023-09-02 11:25:061305browse

C program to print hollow pyramid and rhombus patterns

Here we will see how to generate hollow pyramid and diamond patterns using C language. We can easily generate a solid pyramid pattern. To make it hollow, we need to add a few tricks.

Hollow Pyramid

For the pyramid in the first row, it will print one asterisk and n asterisks in the last row. For other lines, it will print two asterisks at the beginning and end of the line, with some space between the two asterisks.

Sample Code

#include <stdio.h>
int main() {
   int n, i, j;
   printf("Enter number of lines: ");
   scanf("%d", &n);
   for(i = 1; i<=n; i++) {
      for(j = 1; j<=(n-i); j++){ //print the blank spaces before star
         printf(" ");
      }
      if(i == 1 || i == n){ //for the first and last line, print the
         stars continuously
         for(j = 1; j<=i; j++) {
            printf("* ");
         }
      } else {
         printf("*"); //in each line star at start and end
         position
         for(j = 1; j<=2*i-3; j++) { //print space to make hollow
            printf(" ");
         }
         printf("*");
      }
      printf("</p><p>");
   }
}

Output

Enter number of lines: 20
                                         *
                                       *   *
                                      *     *
                                     *       *
                                    *         *
                                   *           *
                                  *             *
                                 *               *
                                *                 *
                               *                   *
                              *                     *
                             *                       *
                            *                         *
                           *                           *
                          *                             *
                         *                               *
                        *                                 *
                       *                                   *
                      *                                     *
                     * * * * * * * * * * * * * * * *  * * *  * 

Hollow Diamond

For the first and last row of diamonds, it will print a star. For other lines it will print two stars at the beginning and end of the line and there will be some space between the two beginnings. A diamond has two parts. Upper and lower halves. In the upper half we have to increase the number of spaces, and in the lower half we have to decrease the number of spaces. Here, you can use another variable called mid to split the line number into two parts.

Sample code

#include <stdio.h>
int main() {
   int n, i, j, mid;
   printf("Enter number of lines: ");
   scanf("%d", &n);
   if(n %2 == 1) { //when n is odd, increase it by 1 to make it even
      n++;
}
mid = (n/2);
for(i = 1; i<= mid; i++) {
    for(j = 1; j<=(mid-i); j++){ //print the blank spaces before star
      printf(" ");
    }
    if(i == 1) {
      printf("*");
    } else {
      printf("*"); //in each line star at start and end
      position
      for(j = 1; j<=2*i-3; j++){ //print space to make hollow
          printf(" ");
      }
      printf("*");
    }
    printf("</p><p>");
}
for(i = mid+1; i<n; i++) {
   for(j = 1; j<=i-mid; j++) { //print the blank spaces before star
      printf(" ");
   }
   if(i == n-1) {
      printf("*");
   } else {
      printf("*"); //in each line star at start and end
      position
      for(j = 1; j<=2*(n - i)-3; j++) { //print space to make
        hollow
        printf(" ");
      }
      printf("*");
   }
   printf("</p><p>");
}

Output

Enter number of lines: 15
                      *
                    *   *
                  *       *
                 *         *
                *           *
               *             *
              *               *
             *                 *
              *               *
               *             *
                *           *
                 *         *
                   *     *
                    *   *
                      *

The above is the detailed content of C program to print hollow pyramid and rhombus patterns. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete