The main thing required in creating a pattern in C is understanding how to use nested loops properly and knowing how the characters in pattern changes.
Here are a few examples to increase your understanding of patterns.
- C Program to create rectangle pattern using stars
- C Program to create rectangle pattern using digits
- C Program to create rectangle pattern using digits – II
- C Program to create right-angled triangle pattern
- C Program to create right angled triangle using alphabets
- C Program to create inverted right-angled triangle using alphabets
- C Program to create pyramid using stars
- C Program to create plus sign using stars
C Program to create rectangle pattern using stars
***** ***** ***** ***** *****
Source Code
#include<stdio.h> int main() { int row, j; for (row=1; row<=5; row++) { for (j=1; j<=5; j++) { printf("*"); } printf("n"); } return 0; }
C Program to create rectangle pattern using digits
11111 22222 33333 44444 55555
Source Code
#include<stdio.h> int main() { int row,j; for (row=1; row<=5; row++) { for (j=1; j<=5; j++) { printf("%d",row); } printf("n"); } return 0; }
C Program to create rectangle pattern using digits – II
12345 12345 12345 12345 12345
Source Code
#include<stdio.h> int main() { int row, j; for (row=1; row<=5; row++) { for (j=1; j<=5; j++) { printf("%d",j); } printf("n"); } return 0; }
C Program to create right-angled triangle pattern
* ** *** **** *****
Source Code
#include<stdio.h> int main() { int row,j; for (row=1; row<=5; row++) { for (j=1; j<=row; j++) { printf("*"); } printf("n"); } return 0; }
C Program to create right angled triangle using alphabets
A AB ABC ABCD ABCDE
Source Code
#include<stdio.h> int main() { int row,j; for (row='A'; row<='E'; row++) { for (j='A'; j<=row; j++) { printf("%c", j); } printf("n"); } return 0; }
C Program to create inverted right-angled triangle using alphabets
ABCDE ABCD ABC AB A
Source Code
#include<stdio.h> int main() { int row,j; for (row='E'; row>='A'; row--) { for (j='A'; j<=row; j++) { printf("%c",j); } printf("n"); } return 0; }
C Program to create pyramid using stars
* *** ***** ******* *********
Source Code
#include <stdio.h> int main() { int min_star=1; //can be changed to set desired minimum numbers of stars in the pattern int star_height=5; //can be changed to increase or decrease height of the pattern int star_space = star_height-1; int i, j, k; for (i=0; i<star_height; i++) { for (j= star_space; j>i; j--) { printf(" "); } for (k=0;k<min_star;k++) { printf("*"); } min_star=min_star+2; printf("n"); } return 0; }
C Program to create plus sign using stars
* * ***** * *
Source Code
#include <stdio.h> int main() { int row, j, k; for (row=1; row<=5; row++) { if (row==3) { for (k=1;k<=5;k++) { printf ("*"); } printf ("n"); continue; } for (j=1; j<3; j++) { printf (" "); } printf ("*"); printf("n"); } return 0; }