Printing patterns is a common task in programming, often used to test one’s understanding of loops and conditional statements. In this article, we will explore various C Program to Print Pyramids and Patterns. We will include multiple variations, each explained with real examples, solutions, and outputs.
Prerequisites
Before diving into the examples, ensure you have the following prerequisites:
- Basic understanding of C programming.
- Familiarity with loops and conditional statements.
- A C compiler installed on your system (e.g., GCC).
Pyramid Patterns
1.1 Explanation
A pyramid pattern is a symmetrical pattern with a triangular shape. Each level of the pyramid has an increasing number of stars or other characters, centered with spaces.
1.2 Program: Simple Pyramid
This program prints a simple pyramid of stars.
#include <stdio.h>
int main() {
int n, i, j, space;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (space = 1; space <= n - i; space++) {
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
1.3 Output
Enter the number of rows: 5
*
***
*****
*******
*********
2.1 Program: Number Pyramid
This program prints a pyramid with numbers.
#include <stdio.h>
int main() {
int n, i, j, space, num;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
num = 1;
for (space = 1; space <= n - i; space++) {
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
printf("%d", num);
num++;
}
printf("\n");
}
return 0;
}
2.2 Output
Enter the number of rows: 5
1
123
12345
1234567
123456789
3.1 Program: Inverted Pyramid
This program prints an inverted pyramid of stars.
#include <stdio.h>
int main() {
int n, i, j, space;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = n; i >= 1; i--) {
for (space = 0; space < n - i; space++) {
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
3.2 Output
Enter the number of rows: 5
*********
*******
*****
***
*
Other Patterns
4.1 Program: Right-angled Triangle
This program prints a right-angled triangle of stars.
#include <stdio.h>
int main() {
int n, i, j;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
4.2 Output
Enter the number of rows: 5
*
**
***
****
*****
5.1 Program: Diamond Pattern
This program prints a diamond pattern of stars.
#include <stdio.h>
int main() {
int n, i, j, space;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (space = 1; space <= n - i; space++) {
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}
for (i = n - 1; i >= 1; i--) {
for (space = 1; space <= n - i; space++) {
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
5.2 Output
Enter the number of rows: 5
*
***
*****
*******
*********
*******
*****
***
*
Conclusion
Using C Program to Print Pyramids and Patterns is an excellent way to practice and understand loops and nested loops. We have explored various examples of pyramid patterns and other shapes, demonstrating different methods to achieve the desired outputs. By practicing these patterns, you will improve your problem-solving skills and gain a deeper understanding of C programming constructs.