While writing the logic for patterns, we use loops in C to generate patterns. We can generate a specific pattern with numbers, characters, and symbols. Pattern program in C can be very useful in understanding loops, conditional statements, and other programming concepts. It can help you freshen up your nested loop concepts.
In this article, we will cover various pattern program in C, such as pyramid patterns, square patterns, Right angle triangles, etc. These patterns are also crucial from an interview point of view. Read the complete article to know more.
Pattern Program in C
C language is a general-purpose language that was created by Dennis Ritchie in 1972. C language is easy to use. Simple keywords, library support, and vast communities are some of the essential features of C language. We can implement the concept of loops and conditional statements to make a pattern program. We can make various geometrical shapes using a pattern program, such as start patterns, pyramid patterns, square patterns, rectangle patterns, etc. Read the complete article to learn more.
Types of Pattern Program in C
We can present our program with numbers, special characters, and others. Let us learn about pattern program in C and their implementation using C language.
1. Pyramid Pattern Program in C
This program will print the pyramid shape using C language. We will ask users to enter the number of rows they want in their pattern.
| Pyramid Pattern Program in C |
| #include <stdio.h>
int main() {
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
return 0;
} |
Output:
2. Inverted Pyramid Pattern program in C
This program will print the inverted pyramid shape using the C language. We will ask users to enter the number of rows they want in their pattern.
| Inverted Pyramid Pattern program in C |
| #include <stdio.h>
int main() {
int rows, i, j, space;
printf ("Enter the number of rows: ");
scanf ("%d", &rows);
for (i = rows; i >= 1; --i) {
for (space = 0; space < rows - i; ++space)
printf (" ");
for (j = i; j <= 2 * i - 1; ++j)
printf ("* ");
for (j = 0; j < i - 1; ++j)
printf("* ");
printf("\n");
}
return 0;
} |
Output
3. Floyd’s Triangle Pattern program in C
This program will print Floyd’s triangle shape using the C language. We will ask users to enter the number of rows they want in their pattern.
| Floyd’s Triangle Pattern program in C |
| #include <stdio.h>
int main() {
int rows, i, j, number = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; ++j) {
printf("%d ", number);
++number;
}
printf("\n");
}
return 0;
} |
Output:
4. Right Half Pyramid Pattern Program in C
This program will print Right half pyramid triangle shape using the C language. We will ask users to enter the number of rows they want in their pattern.
| Right Triangle Pattern program in C |
| #include <stdio.h>
int main()
{
int rows = 7;
// first loop for printing rows
for (int i = 0; i < rows; i++) {
// second loop for printing character in each rows
for (int j = 0; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
} |
Output:
5. Diamond Pyramid Pattern Program in C
This program will print Right half pyramid triangle shape using the C language. We will ask users to enter the number of rows they want in their pattern.
| Diamond Pattern program in C |
| #include <stdio.h>int main()
{
int n;
printf ("Enter the number of rows: ");
scanf ("%d", &n);
// first outer loop to iterate through each row
for (int i = 0; i < 2 * n - 1; i++) {
int comp;
if (i < n) {
comp = 2 * (n - i) - 1;
}
else {
comp = 2 * (i - n + 1) + 1;
}
// first inner loop to print leading whitespaces
for (int j = 0; j < comp; j++) {
printf(" ");
}
for (int k = 0; k < 2 * n - comp; k++) {
printf("* ");
}
printf("\n");
}
return 0;
} |
Also read: Keywords and Identifiers in C
Output:
6. Hollow Pyramid Pattern Program in C
This program will print the Hollow pyramid triangle shape using the C language. We will ask users to enter the number of rows they want in their pattern.
| Hollow Pyramid Pattern program in C |
| #include <stdio.h>
int main() {
int rows;
printf("Enter the number of rows: ");
scanf("%d",&rows);
// first outer loop to iterate through each row
for (int i = 0; i < rows; i++) {
// first inner loop to print leading whitespaces
for (int j = 0; j < rows - i - 1; j++) {
printf(" ");
}
// second inner loop to print stars * and inner whitespaces
for (int k = 0; k < 2 * i + 1; k++) {
if (k == 0 || k == 2 * i) {
printf("*");
} else if (i == rows - 1) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
} |
Also read: Linear Search Algorithm in C
Output:
7. Right Triangle Pattern Program in C
This program will print the Right Triangle pyramid triangle shape using characters in the C language. We will ask users to enter the number of rows they want in their pattern.
| Right Triangle Pyramid Pattern program in C |
| #include<stdio.h>
int main()
{
int n;
printf("Enter the number of rows: ");
scanf("%d",&n);
int k = 0;
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= i; j++)
{
if((i + j) % 2 == 1)
{
printf("%c ",(char)(k + 97));
}
else
{
printf("%c ",(char)(k + 65));
}
k++;
}
printf("\n");
}
return 0;
} |
Output: