Solving pattern programs in C is the best way to improve our iteration logic and programming skills. Here in this post, I have listed 20 different patterns with solutions in the C programming language.
Pattern 1:
* *** ***** ******* *********
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=1; i<=MAX; i++){
for(int j=1; j<=MAX-i; j++){
printf(" ");
}
for(int k=1; k<2*i; k++){
printf("*");
}
printf("\n");
}
return 0;
}
Pattern 2:
1 222 33333 4444444 555555555
#include <stdio.h>
#define MAX 5
int main()
{
int i,j,k;
for(int i=1; i<=MAX; i++){
for(int j=1; j<=MAX-i; j++){
printf(" ");
}
for(int k=1; k<2*i; k++){
printf("%d",i);
}
printf("\n");
}
return 0;
}
Pattern 3:
* ** *** **** *****
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=1; i<=MAX; i++){
for(int j=1; j<=MAX-i; j++){
printf(" ");
}
for(int k=1; k<=i; k++){
printf("*");
}
printf("\n");
}
return 0;
}
Pattern 4:
1
22
333
4444
55555
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=1; i<=MAX; i++){
for(int j=1; j<=MAX-i; j++){
printf(" ");
}
for(int k=1; k<=i; k++){
printf("%d",i);
}
printf("\n");
}
return 0;
}
Pattern 5:
********* ******* ***** *** *
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=1; i<=MAX; i++){
for(int j=1; j<=i; j++){
printf(" ");
}
for(int k=1; k<=2*(MAX-i)+1; k++){
printf("*");
}
printf("\n");
}
return 0;
}
Pattern 6:
111111111 2222222 33333 444 5
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=1; i<=MAX; i++){
for(int j=1; j<=i; j++){
printf(" ");
}
for(int k=1; k<=2*(MAX-i)+1; k++){
printf("%d",i);
}
printf("\n");
}
return 0;
}
Pattern 7:
1 12 123 1234 12345
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=1; i<=MAX; i++){
for(int j=1; j<=i; j++){
printf("%d",j);
}
printf("\n");
}
return 0;
}
Pattern 8:
1 22 333 4444 55555
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=1; i<=MAX; i++){
for(int j=1; j<=i; j++){
printf("%d",i);
}
printf("\n");
}
return 0;
}
Pattern 9:
11111 2222 333 44 5
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=1; i<=MAX; i++){
for(int j=1; j<=MAX-i+1; j++){
printf("%d",i);
}
printf("\n");
}
return 0;
}
Pattern 10:
12345 1234 123 12 1
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=1; i<=MAX; i++){
for(int j=1; j<=MAX-i+1; j++){
printf("%d",j);
}
printf("\n");
}
return 0;
}
Pattern 11:
54321 5432 543 54 5
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=MAX; i>=0; i--){
for(int j=MAX; j>MAX-i; j--){
printf("%d",j);
}
printf("\n");
}
return 0;
}
Pattern 12:
5 54 543 5432 54321
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=MAX; i>=1; i--){
for(int j=MAX; j>=i; j--){
printf("%d",j);
}
printf("\n");
}
return 0;
}
Pattern 13:
1 121 12321 1234321 123454321
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=1; i<=MAX; i++){
//For left Space
for(int j=1; j<=MAX-i; j++){
printf(" ");
}
//1st middle half
for(int k=1; k<=i; k++){
printf("%d",k);
}
//2nd middle half
for(int k=i-1; k>=1; k--){
printf("%d",k);
}
printf("\n");
}
return 0;
}
Pattern 14:
1 21 321 4321 54321
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=1; i<=MAX; i++){
for(int j=i; j>=1; j--){
printf("%d",j);
}
printf("\n");
}
return 0;
}
Pattern 15:
1 10 101 1010 10101
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=1; i<=MAX; i++){
for(int j=1; j<=i; j++){
//Output remainder by dividing with 2
printf("%d", j%2);
}
printf("\n");
}
return 0;
}
Pattern 16:
101010 010101 101010 010101 101010
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=1; i<=MAX; i++){
for(int j=i; j<=MAX+i; j++){
printf("%d",j%2);
}
printf("\n");
}
return 0;
}
Pattern 17:
10000 02000 00300 00040 00005
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=1; i<=MAX; i++){
for(int j=1; j<=MAX; j++){
if(i== j)
printf("%d",i);
else
printf("%d",0);
}
printf("\n");
}
return 0;
}
Pattern 18:
11111 01111 00111 00011 00001
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=1; i<=MAX; i++){
for(int j=1; j<=MAX; j++){
if(i<= j)
printf("%d",1);
else
printf("%d",0);
}
printf("\n");
}
return 0;
}
Pattern 19:
1 2 3 4 5
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=1; i<=MAX; i++){
//For left blank space
for(int j=1; j<i; j++){
printf(" ");
}
//Output single digit 'i'
printf("%d \n",i);
}
return 0;
}
Pattern 20:
1 2 3 4 5
#include <stdio.h>
#define MAX 5
int main()
{
for(int i=1; i<=MAX; i++){
//For left blank space
for(int j=i; j<MAX; j++){
printf(" ");
}
//Output single digit 'i'
printf("%d \n",i);
}
return 0;
}
These are different pattern programs that are often asked in exams and interviews. I advise you to try to solve the pattern by yourself, if stuck then refer to its solution.
Comment below the pattern that is not mentioned here, I will add it to the above list.