C Program to Print Pyramid Patterns.

Patterns help us understand valuable insights into the logic and control flow required in programming. In this C programming tutorial, we are going to learn how to print different kinds of Pyramid patterns.

Here we are going to see 6 different kinds of Pyramid patterns with a C code for each of them:

Pattern 1: Left-aligned Pyramid Pattern.

Below is the C program to print a left-aligned pyramid pattern.
#include <stdio.h>

int main() {
    int n;

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

    printf("\nPattern 1:\n");
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows for the pyramid pattern: 5
Pattern 1:
* 
* * 
* * * 
* * * * 
* * * * * 


Pattern 2: Inverted Left-aligned Pyramid Pattern.

Below is the C program to print an Inverted left-aligned pyramid pattern.
#include <stdio.h>

int main() {
    int n;

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

    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows for the pyramid pattern: 5
* * * * * 
* * * * 
* * * 
* * 
* 

Pattern 3: Center-aligned Pyramid Pattern.

Below is the C program to print a Center-aligned pyramid pattern.
#include <stdio.h>

int main() {
    int n;

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

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++) {
            printf("  ");
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows for the pyramid pattern: 5
        * 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * * 

Pattern 4: Inverted Center-aligned Pyramid Pattern.

Below is the C program to print an Inverted Center-aligned pyramid pattern.
#include <stdio.h>

int main() {
    int n;

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

    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= n - i; j++) {
            printf("  ");
        }
        for (int j = 1; j <= 2 * i - 1; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows for the pyramid pattern: 6
* * * * * * * * * * * 
  * * * * * * * * * 
    * * * * * * * 
      * * * * * 
        * * * 
          * 


Pattern 5: Right-aligned Pyramid Pattern.

Below is the C program to print a Right-aligned pyramid pattern.
#include <stdio.h>

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

    for (int i = 1; i <= rows; i++) {
        for (int space = 1; space <= rows - i; space++) {
            printf(" ");
        }
        for (int j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows: 5
    *
   **
  ***
 ****
*****


Pattern 6: Number Pyramid Pattern.

Below is the C program to print a Number pyramid pattern.
#include <stdio.h>

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

    for (int i = 1; i <= rows; i++) {
        for (int space = 1; space <= rows - i; space++) {
            printf(" ");
        }
        for (int j = 1; j <= i; j++) {
            printf("%d", j);
        }
        for (int j = i - 1; j >= 1; j--) {
            printf("%d", j);
        }
        printf("\n");
    }

    return 0;
}
Output:
Enter the number of rows: 5
    1
   121
  12321
 1234321
123454321

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS