C Pyramid Pattern Program


A Pyramid Pattern Program in C is a common exercise that demonstrates the use of nested loops to create a visually appealing arrangement of characters, typically letters or asterisks, in a pyramid shape. This type of program is helpful for understanding loops and how to manage spaces and characters in console output.

Example: Pyramid Pattern with Asterisks

Let’s consider a pyramid pattern of asterisks. If the user inputs the number of rows as 5, the output will look like this:

* *** ***** ******* *********

Logic of the Program:

  1. Take an integer input from the user that represents the number of rows for the pyramid.
  2. Use a nested loop:
    • The outer loop will iterate through each row.
    • The inner loops will handle the spaces and the asterisks for each row.
  3. Print spaces before the asterisks to align the pyramid correctly.
  4. Print the asterisks for each row.
  5. Move to the next line after printing each row.

C Program to Generate a Pyramid Pattern:

#include <stdio.h> int main() { int rows, i, j, space; // Input the number of rows from the user printf("Enter the number of rows: "); scanf("%d", &rows); // Generate the pyramid pattern for (i = 1; i <= rows; i++) { // Outer loop for each row // Print spaces for (space = 1; space <= rows - i; space++) { printf(" "); // Print space for alignment } // Print asterisks for (j = 1; j <= (2 * i - 1); j++) { printf("*"); // Print asterisk } printf("\n"); // Move to the next line after each row } return 0; }

Explanation:

  1. Variables:

    • int rows: Holds the number of rows for the pyramid.
    • int i, j, space: Loop counters.
  2. Input:

    • The program prompts the user to enter the number of rows for the pyramid.
  3. Outer Loop:

    • The outer loop (for (i = 1; i <= rows; i++)) controls the number of rows. It runs from 1 to rows.
  4. Inner Loop for Spaces:

    • The first inner loop (for (space = 1; space <= rows - i; space++)) prints spaces before the asterisks. The number of spaces decreases with each row, calculated as rows - i.
  5. Inner Loop for Asterisks:

    • The second inner loop (for (j = 1; j <= (2 * i - 1); j++)) prints the asterisks. The number of asterisks increases with each row, calculated as 2 * i - 1. This ensures that for each row, you have an odd number of asterisks, which is essential for a symmetrical pyramid.
  6. Newline:

    • After printing the spaces and asterisks for each row, a newline (printf("\n")) is printed to move to the next line.

Sample Output:

Example 1:

Enter the number of rows: 5 * *** ***** ******* *********

Example 2:

Enter the number of rows: 3 * *** *****

Key Points:

  • Nested Loops: The program demonstrates the use of nested loops effectively to manage both spaces and characters.
  • Dynamic Input: The number of rows can be modified by the user, making the program versatile.
  • Alignment: Proper alignment using spaces ensures that the pyramid appears correctly shaped.

Variations:

You can modify the program to create different pyramid patterns, such as:

  • Number Pyramids: Replace asterisks with numbers.
  • Inverted Pyramids: Change the logic to create a downward pyramid.
  • Hollow Pyramids: Print asterisks only on the borders of the pyramid.

By changing the loops and conditions, you can create a wide variety of interesting patterns.