Patterm Program in C
The C programming language has many patterns, including character, numeric, and star patterns. Using examples, we will review how to design various patterns in this part.
THE NUMBERED PATTERNS:
Example 1: Program in C to print the number pyramid pattern
This section will demonstrate how to print various number patterns using the C programming language, a C program to print the number pyramid pattern as an example,
The user can provide the desired number of rows in the following C program to print the number pyramid pattern, and the output will be shown:
#include<stdio.h> int main() { int n, x, y, k; printf("Enter the number of rows to show number pattern: "); scanf("%d",&n); for(x =1; x <= n; x++) { for(y =1; y <= n; y++) { if(y <= x) printf("%d",y); else printf(" "); } for(y = n; y >= 1;y--) { if(y <= x) printf("%d",y); else printf(" "); } printf("\n"); } return 0; }
OUTPUT:
Example 2: Program in C to print the number of diamond patterns:
The user can choose the desired number of rows to print the diamond pattern in the following C program, and the output will be shown on the screen.
#include<stdio.h> int main() { int n, x, y, k; printf("Enter the number of rows to show number paatern: "); scanf("%d",&n); for(x = 1; x <= n; x++) { for(y = x; y <n; y++) { printf(" "); } for(k = 1; k < (x*2); k++) { printf("%d",k); } printf("\n"); } for(x = 4; x >= 1; x--) { for(y = n; y > x; y--) { printf(" "); } for(k = 1; k < (x*2); k++) { printf("%d",k); } printf("\n"); } return 0; }
OUTPUT:
Example 3. Program in C to print the number pyramid pattern:
The user can specify the number of rows to print the number pyramid pattern in the following C program, and the desired outcome will be shown on the screen:
#include<stdio.h> int main() { int x, s, n, y = 0, cntr = 0, cntr1 = 0; printf("Enter the number of rows to show number pattern: "); scanf("%d",&n); for(x = 1; x <= n; ++x) { for(s = 1; s <= n-x; ++s) { printf(" "); ++cntr; } while(y != 2 * x - 1) { if (cntr <= n - 1) { printf("%d ", x + y); ++cntr; } else { ++cntr1; printf("%d ", (x + y - 2 * cntr1)); } ++y; } cntr1 = cntr = y = 0; printf("\n"); } return 0; }
OUTPUT:
Example 4. Program in C to print the vertical number pattern:
The user can specify the number of rows to print the vertical number pyramid pattern in the following C program, and the output will be shown on the screen.
#include<stdio.h> int main() { int n, x, y; printf("Enter the number of rows to show number pattern: "); scanf("%d",&n); for(int x = 1; x < n; x++) { for(int y = 1; y <= x; y++) printf("%d",y); printf("\n"); } for(int x = n; x >= 0; x--) { for(int y = 1; y <= x; y++) printf("%d",y); printf("\n"); } return 0; }
OUTPUT:
Example 5. Program in C to print the number pyramid pattern:
The user can provide the desired number of rows in the following C program to print the number pyramid pattern, and the output will be shown on the screen.
#include<stdio.h> int main() { int n, x, y; printf("Enter the number of rows to show number patterns: "); scanf("%d",&n); for (int x = n; x >= 0; x--) { for (int y = 1; y <= x; y++) printf("%d",y); printf("\n"); } for(int x = 1; x <= n; x++) { for(int y = 1; y <= x; y++) printf("%d",y); printf("\n"); } return 0; }
OUTPUT:
CONCLUSION:
In conclusion, growing pattern packages in C is an terrific approach to hone your programming abilities and higher recognize how conditional statements and loops perform. They also are an innovative and interesting way to check your programming talents.
Breaking the sample down into smaller pieces, using nested loops to iterate through the rows and columns, and using conditional statements to pick out what to output at every point are a few pointers for writing pattern programs in C. It also enables to experiment with numerous styles and variations to enhance your information and creativity.