If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In C, patterns are specific arrangements of characters that can be printed to the console using loops and control flow statements. Patterns can be used for visual representation of data or for creating a user interface. Here are a few examples of patterns that can be created using C:
Copy code
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
This code uses nested loops to print a triangle pattern of asterisks. The outer loop controls the number of rows and the inner loop controls the number of asterisks in each row.
Copy code
int k = 0;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5 - i; j++) {
printf(" ");
}
while (k != (2 * i - 1)) {
printf("* ");
k++;
}
k = 0;
printf("\n");
}
This code uses nested loops and a while loop to print a pyramid pattern of asterisks. The outer loop controls the number of rows and the inner loop controls the number of spaces before the asterisks. The while loop controls the number of asterisks in each row.
Copy code
int k = 0;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5 - i; j++) {
printf(" ");
}
while (k != (2 * i - 1)) {
printf("* ");
k++;
}
k = 0;
printf("\n");
}
for (int i = 5 - 1; i >= 1; i--) {
for (int j = 1; j <= 5 - i; j++) {
printf(" ");
}
k = 0;
while (k != (2 * i - 1)) {
printf("* ");
k++;
}
printf("\n");
}
This code uses nested loops, a while loop, and a second loop to print a diamond pattern of asterisks. The first loop creates the upper half of the diamond and the second loop creates the lower half of the diamond.
These are just a few examples of patterns that can be created in C. There are many other patterns that can be created using different combinations of loops, control flow statements and characters.
Comments: 0