Slider

C Program to Print Pyramid Patterns.

Here we are going to see 6 different kinds of Pyramid patterns with a C code for each of them: C program to print a left-aligned pyramid C program to
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

0

No comments

Post a Comment

both, mystorymag

DON'T MISS

Tech News
© all rights reserved
made with by AlgoLesson
Table of Contents