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; }
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; }
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; }
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; }
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; }
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; }
Enter the number of rows: 5
1
121
12321
1234321
123454321
No comments:
Post a Comment