Saturday, 8 August 2026

Pyramid PAttern in C



Option 1: Simple Star Pyramid in C
#include <stdio.h>

int main() {
    
    // Variable to store number of rows
    int rows;  

    // Take input from user
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    // Outer loop for rows
    for (int i = 1; i <= rows; i++) {

        // Print spaces before stars
        for (int j = i; j < rows; j++) {
            printf(" ");
        }

        // Print stars (2*i - 1 for pyramid shape)
        for (int k = 1; k <= (2 * i - 1); k++) {
            printf("*");
        }

        // Move to next line after each row
        printf("\n");
    }

    return 0;  
}

Output

Enter the number of rows: 4
   *
  ***
 *****
*******

Explanation

A star pyramid in C is one of the most basic and widely practiced pyramid patterns. This program uses nested loops: the outer loop manages rows, while two inner loops manage spaces and stars respectively. 

By adjusting these loops, we ensure the stars align symmetrically to form a pyramid. Understanding this pyramid program in C language helps you build logic for more complex patterns. 

This program introduces the use of spaces to align stars properly, making it foundational for mastering pyramid patterns in C.


Time Complexity Analysis

Time Complexity : O(n²)

Space Complexity Analysis

Space Complexity : O(1)

Option 2: Pyramid Pattern of Numbers in C

#include <stdio.h>

int main() {
    int rows; 

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    // Outer loop for rows
    for (int i = 1; i <= rows; i++) {

        // Print spaces for alignment
        for (int j = i; j < rows; j++) {
            printf(" ");
        }

        // Print numbers from 1 to i
        for (int k = 1; k <= i; k++) {
            printf("%d ", k);
        }

        // Move to next line
        printf("\n");  
    }

    return 0;
}

Output

Enter the number of rows: 7

      1 
     1 2 
    1 2 3 
   1 2 3 4 
  1 2 3 4 5 
 1 2 3 4 5 6 
1 2 3 4 5 6 7 

Explanation

A number pyramid pattern in C programming displays numbers instead of stars in a pyramid structure. The logic involves printing numbers incrementally in each row, using nested loops. 

The first inner loop prints spaces, while the second loop prints numbers up to the current row count. 

This pyramid code in C is helpful for enhancing your understanding of both number sequences and alignment logic, making it a valuable practice for beginners.

Time Complexity Analysis

Time Complexity : O(n²)

Space Complexity Analysis

Space Complexity : O(1)

Option 3: Full Pyramid Pattern in C

#include <stdio.h>

int main() {
    int rows;  

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    // Loop through each row
    for (int i = 1; i <= rows; i++) {

        // Print spaces for alignment
        for (int j = i; j < rows; j++) {
            printf(" ");
        }

        // Print stars in pyramid form
        for (int k = 1; k <= (2 * i - 1); k++) {
            printf("*");
        }

        printf("\n");  
    }

    return 0;
}

Output

Enter the number of rows: 4
   *
  ***
 *****
*******

Explanation

A full pyramid pattern in C is a combination of a star pyramid with precise spacing to form a symmetrical structure. Similar to the basic star pyramid, this pattern uses nested loops to manage rows, spaces, and stars, but adds a balance between both left and right sides of the pyramid. 

This type of pyramid pattern in C shows the use of arithmetic calculations in loops for perfect alignment.

Time Complexity Analysis

Time Complexity : O(n²)

Space Complexity Analysis

Space Complexity : O(1)

Option 4: Hollow Pyramid Pattern in C

#include <stdio.h>

int main() {
    int rows;  

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    // Outer loop for rows
    for (int i = 1; i <= rows; i++) {

        // Print spaces
        for (int j = i; j < rows; j++) {
            printf(" ");
        }

        // Print stars or spaces
        for (int k = 1; k <= (2 * i - 1); k++) {

            // Print star only at boundaries or last row
            if (k == 1 || k == (2 * i - 1) || i == rows) {
                printf("*");
            } else {
                
                // Inner hollow space
                printf(" ");  
            }
        }

        // Next line
        printf("\n");  
    }

    return 0;
}

Output

Enter the number of rows: 7
      *
     * *
    *   *
   *     *
  *       *
 *         *
*************

Explanation

A hollow pyramid pattern in C adds complexity by printing only the border of the pyramid while keeping the inside hollow. It uses conditional statements inside the loops to determine whether to print a star (*) or a space. 

This program enhances your problem-solving skills and introduces conditional logic within nested loops.

Time Complexity Analysis

Time Complexity : O(n²)

Space Complexity Analysis

Space Complexity : O(1)

Option 5: Inverted Pyramid Program in C

#include <stdio.h>

int main() {
    int rows;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (int i = rows; i >= 1; i--) {
        // Print spaces
        for (int j = i; j < rows; j++) {
            printf(" ");
        }
        // Print stars
        for (int k = 1; k <= (2 * i - 1); k++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Output

Enter the number of rows: 4
*******
 *****
  ***
   *

Explanation

An inverted pyramid in C programming reverses the structure of the star pyramid. It starts with the maximum stars in the first row and decreases in subsequent rows. 

The program uses nested loops to print spaces first, followed by the stars. This pyramid program in C language is a good exercise for understanding reverse iteration.

Time Complexity Analysis

Time Complexity : O(n²)

Space Complexity Analysis

Space Complexity : O(1)

Option 6: Diamond Pattern Program in C

#include <stdio.h>

int main() {
    int rows;  

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    // Upper half (Full Pyramid)
    for (int i = 1; i <= rows; i++) {

        // Print spaces
        for (int j = i; j < rows; j++) {
            printf(" ");
        }

        // Print stars
        for (int k = 1; k <= (2 * i - 1); k++) {
            printf("*");
        }

        printf("\n"); 
    }

    // Lower half (Inverted Pyramid)
    for (int i = rows - 1; i >= 1; i--) {

        // Print spaces
        for (int j = rows; j > i; j--) {
            printf(" ");
        }

        // Print stars
        for (int k = 1; k <= (2 * i - 1); k++) {
            printf("*");
        }

        printf("\n");  // Next line
    }

    return 0;
}

Output

Enter the number of rows: 4
   *
  ***
 *****
*******
 *****
  ***
   *

Explanation

The diamond pattern in C combines a full pyramid and an inverted pyramid. It is created by printing a full pyramid first and then appending an inverted pyramid below it. This pyramid code in C demonstrates how to merge different patterns by reusing logic.

Time Complexity Analysis

Time Complexity : O(n²)

Space Complexity Analysis

Space Complexity : O(1)

Option 7: Half Pyramid Program in C

#include <stdio.h>

int main() {
    int rows = 5;

    // Outer loop for rows
    for (int i = 1; i <= rows; i++) {

        // Print stars equal to row number
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }

        printf("\n");
    }

    return 0;
}

Output

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

Explanation

  • The program sets the number of rows (5) and uses an outer loop to control how many lines of the pattern are printed.

  • Inside it, an inner loop prints stars (*) equal to the current row number, creating an increasing pattern.

  • After each row, a new line is printed, resulting in a right-angled triangle star pattern.

Time Complexity Analysis

Time Complexity : O(n²)

Space Complexity Analysis

Space Complexity : O(1)

Option 8: Floyd’s Triangle Program in C

#include <stdio.h>

int main() {
    int rows = 5, num = 1;

    // Outer loop
    for (int i = 1; i <= rows; i++) {

        // Print numbers continuously
        for (int j = 1; j <= i; j++) {
            printf("%d ", num++);
        }

        printf("\n");
    }

    return 0;
}

Output

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15

Explanation

  • The program sets the number of rows (5) and initializes a variable num to print numbers sequentially.

  • It uses nested loops where the inner loop prints numbers continuously, increasing num after each print.

  • After each row, a new line is printed, forming a number pattern in a triangular shape.

Time Complexity Analysis

Time Complexity : O(n²)

Space Complexity Analysis

Space Complexity : O(1)

Option 9: Palindrome Pyramid Program in C

#include <stdio.h>

int main() {
    int rows = 5;

    for (int i = 1; i <= rows; i++) {

        // Print spaces
        for (int j = i; j < rows; j++) {
            printf(" ");
        }

        // Print increasing numbers
        for (int k = 1; k <= i; k++) {
            printf("%d", k);
        }

        // Print decreasing numbers
        for (int k = i - 1; k >= 1; k--) {
            printf("%d", k);
        }

        printf("\n");
    }

    return 0;
}

Output

    1
   121
  12321
 1234321
123454321

Explanation

  • The program sets the number of rows (5) and uses an outer loop to control the pattern height.

  • It first prints spaces for alignment, then prints increasing numbers followed by decreasing numbers to form a symmetric pyramid pattern.

  • After each row, a new line is printed, resulting in a centered number pyramid.

Time Complexity Analysis

Time Complexity : O(n²)

Space Complexity Analysis

Space Complexity : O(1)

Option 10: Reverse Number Pyramid Program in C

#include <stdio.h>

int main() {
    int rows = 5;

    for (int i = 1; i <= rows; i++) {

        // Print spaces
        for (int j = i; j < rows; j++) {
            printf(" ");
        }

        // Print decreasing numbers
        for (int k = i; k >= 1; k--) {
            printf("%d ", k);
        }

        printf("\n");
    }

    return 0;
}

Output

    1 
   2 1 
  3 2 1 
 4 3 2 1 
5 4 3 2 1

Explanation

  • The program sets the number of rows (5) and uses an outer loop to control the number of lines in the pattern.

  • It first prints spaces for alignment, then prints numbers in decreasing order from the current row value down to 1.

  • After each row, a new line is printed, forming a right-aligned inverted number pattern.

Time Complexity Analysis

Time Complexity : O(n²)

Space Complexity Analysis

Space Complexity : O(1)

Option 11: Alphabet Pyramid Pattern Program in C

#include <stdio.h>

int main() {
    int rows = 5;

    for (int i = 1; i <= rows; i++) {

        // Print spaces
        for (int j = i; j < rows; j++) {
            printf(" ");
        }

        // Print alphabets
        for (int k = 0; k < i; k++) {
            printf("%c ", 'A' + k);
        }

        printf("\n");
    }

    return 0;
}

Output

    A 
   A B 
  A B C 
 A B C D 
A B C D E 

Explanation

  • The program sets the number of rows (5) and uses an outer loop to control the pattern height.

  • It first prints spaces for alignment, then prints alphabets starting from 'A' by incrementing characters using 'A' + k.

  • After each row, a new line is printed, forming a right-aligned alphabet pattern.

Time Complexity Analysis

Time Complexity : O(n²)

Space Complexity Analysis

Space Complexity : O(1)


No comments:

Post a Comment