Hollow Pyramid Star Pattern
Program to Print Hollow Pyramid Star Pattern
PRINTING PATTERN:
* * * * * *******
PREREQUISITE:
Basic knowledge of C language and loops.
ALGORITHM:
1. Take the number of rows as input from the user (height of the pyramid) and store it in a variable ('n').
2. Run a loop 'n' times to iterate through the rows (i=1 to i<=n).
3. For each row, print spaces to create the left alignment of the stars.
4. For the first and last rows, print stars for the entire row.
5. For the rows in between, print a star at the beginning and end, and spaces in between.
6. Move to the next line after each row has been printed.
Code in C:
#include <stdio.h> int main() { int n, i, j; // Declaring integers for rows and loop counters printf("Enter the number of rows for the hollow pyramid:\n"); scanf("%d", &n); // Taking input for number of rows for (i = 1; i <= n; i++) { // Print spaces for pyramid shape for (j = n - i; j > 0; j--) { printf(" "); // Printing spaces } // Print stars for each row for (j = 1; j <= (2 * i - 1); j++) { // Print '*' for the first and last row, and first and last column if (i == n || j == 1 || j == (2 * i - 1)) { printf("*"); // Printing stars } else { printf(" "); // Printing spaces for hollow part } } printf("\n"); // New line after each row } return 0; }
Output:
Enter the number of rows for the hollow pyramid: 4 * * * * * *******