Program to Print Pyramids and Patterns - (C Programs)
Printing pyramids and patterns is a common programming exercise that helps improve problem-solving skills. In this tutorial, we will discuss a program to print different kinds of pyramids and patterns using C programming language.
Syntax
The syntax for printing pyramids and patterns can vary depending on the specific pattern you want to print. The general syntax for printing patterns in C is as follows:
for (int i = 0; i < rows; i++) {
for (int j = 0; j <= i; j++) {
printf("* ");
}
printf("\n");
}
``## Examples
### Pyramid Pattern 1
*
```c
#include <stdio.h>
int main() {
int rows, i, j, k;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 0; i < rows; i++) {
for (j = 0; j < rows - i; j++) {
printf(" ");
}
for (k = 0; k <= i * 2; k++) {
printf("*");
}
printf("\n");
}
return 0;
}
Output:
Enter the number of rows: 5
*
***
*****
*******
*********
Pyramid Pattern 2
*
***
*****
*******
*********
*******
*****
***
*
#include <stdio.h>
int main() {
int rows, i, j, k;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 0; i < rows; i++) {
for (j = 0; j < rows - i; j++) {
printf(" ");
}
for (k = 0; k <= i * 2; k++) {
printf("*");
}
printf("\n");
}
for (i = rows - 2; i >= 0; i--) {
for (j = 0; j < rows - i; j++) {
printf(" ");
}
for (k = 0; k <= i * 2; k++) {
printf("*");
}
printf("\n");
}
return 0;
}
Output:
Enter the number of rows: 5
*
***
*****
*******
*********
*******
*****
***
*
Explanation
Pyramid and pattern printing programs in C generally involve a nested loop structure. The outer loop controls the number of rows, while the inner loop controls the number of asterisks or spaces printed on each line. By manipulating the number of asterisks or spaces printed on each line, we can create different pyramid and pattern shapes.
Use
Printing pyramid and pattern programs in C are great for practicing problem-solving skills and improving coding abilities. They help programmers in developing a better understanding of loops, conditional statements, and how to manipulate variables to create desired patterns.
Summary
In this tutorial, we have discussed a program to print different types of pyramid and pattern shapes in C programming language. We have seen the syntax, examples, explanation, and use of printing pyramid and pattern programming in C. By practicing these exercises, programmers can improve their problem-solving skills and become better equipped to tackle complex coding challenges.