There are many different patterns that can be printed using C programming. Here are a few examples:
1. Right-angle triangle
C
#include <stdio.h>
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output:
Code snippet
Enter the number of rows: 5
*
**
***
****
*****
2. Inverted right-angle triangle
C
#include <stdio.h>
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
printf(" ");
}
for (int j = 0; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output:
Code snippet
Enter the number of rows: 5
*
**
***
****
*****
3. Arrow-shaped pattern
C
#include <stdio.h>
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
printf(" ");
}
for (int j = 0; j <= i; j++) {
printf("* ");
}
printf("\n");
}
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < n - i - 1; j++) {
printf(" ");
}
for (int j = 0; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output:
Code snippet
Enter the number of rows: 5
*
**
***
****
*****
*****
****
***
**
*
These are just a few examples of the many patterns that can be printed using C programming. You can experiment with different patterns and see what you can create.