Patterns in C

Patterns in C : This article is a complete source code library for every pattern program in C like number pattern programs, star (*) pattern programs, triangle or pyramid pattern programs, alphabet pattern programs.


Diamond Pattern in C

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
Here is the source code of c program to print or the display diamond pattern using stars.
#include<stdio.h>

int main()
{
  int i,n,j,s=1;

  printf("\n Enter number of rows:- ");
  scanf("%d",&n);

  printf("\n This is the diamond pattern in stars: \n");

  s=n-1;

  for(j=1;j<=n;j++)
  {
	for(i=1;i<=s;i++)
	  printf(" ");

	s--;

	for(i=1;i<=2*j-1;i++)
	  printf("*");

	printf("\n");
  }

	s=1;

  for(j=1;j<=n-1;j++)
  {
	for(i=1;i<=s;i++)
	  printf(" ");

	s++;

	for(i=1;i<=2*(n-j)-1;i++)
	  printf("*");

	printf("\n");

  }

  return 0;
}

/* Output of above code:-

 Enter number of rows:- 5

 This is the diamond pattern in stars:

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

*/
If you want the algorithm or general idea behind how to print diamond pattern in C visit - C program to print diamond pattern

Print Pascal's Triangle in C

           1
         1   1
       1   2   1
     1   3   3    1
   1  4    6   4   1
 1  5   10   10  5   1

Here is the source code to print Pascal's triangle in C.
#include<stdio.h>

int main()
{
  int i, j, coeff = 1, space;

  printf("\n Enter number of rows:- ");
  scanf(" %d",&rows);

  for(i=0; i<rows; i++)
  {
	for(space=1; space <= rows-i; space++)
		printf("  ");

	for(j=0; j <= i; j++)
	{
		if (j==0 || i==0)
			coeff = 1;
		else
			coeff = coeff*(i-j+1)/j;
		printf("%4d", coef);
	}
	printf("\n");
  }

  return 0;
}

/* Output of above code:-

           1
         1   1
       1   2   1
     1   3   3    1
   1  4    6   4   1
 1  5   10   10  5   1

*/

Program to Print Floyd's Triangle in C

  1
 
  2  3
 
  4  5  6

  7  8  9 10
Here is the source code of C program to print half triangle of numbers.
#include<stdio.h>

int main()
{
  int i,j,c=0;

  printf("\n");

  for(i=1;i<=4;i++)
  {
	for(j=1;j<=i;j++)
		printf(" %d",c++);
		printf("\n \n");	
  }

  return 0;

}

/* Output of above code:-

  1
 
  2  3
 
  4  5  6

  7  8  9  10

*/

Program to Print half Triangle (Pyramid) Pattern using Stars (*) in C

  *
 
  *  *
 
  *  *  *
Here is the source code of C program to print half triangle of stars (*).
#include<stdio.h>

int main()
{
  int i,j,c=0;

  printf("\n");

  for(i=1;i<=3;i++)
  {
	for(j=1;j<=i;j++)
		printf(" %d",c++);
		printf("\n \n");	
  }

  return 0;
}

/* Output of above code:-

  *
 
  *  *
 
  *  *  *

*/

Print Star Triangle or Pyramid in C

    *
   ***
  *****
 *******
*********
Here is the source code of conduct program to print full triangle or pyramid using stars (* 🌟).
#include<stdio.h>

int main()
{
  int row, cols, size, sp;

  printf("\n Enter number of rows to print star triangle:- ");
  scanf("%d", &size);
  
  sp = size;

  for(row = 1; row <= size; row++)
  {
	for (cols = 1; cols < sp; cols++)
	printf(" ");

	sp--;

	for (cols = 1; cols <= 2*row - 1; cols++)
		printf("*");
	printf("\n");
  }
 
  return 0;
}


Program to Print half Triangle (Pyramid) Pattern Using Alphabets in C

A 
B C 
D E F 
Here is the source code of C program to print half triangle of alphabets.
#include<stdio.h>

int main()
{
  int c,a,e,l,num,m;
 
  e=64;
  printf("\n");

  for (l=1;l<=3;l++)
  {
	for (num=e+1;num<=e+l;num++)
		printf("%c\t",num);
		printf("\n");
	e=e+l;
  }

  printf("\n");

}

/* Output of above code:-

A 
B C 
D E F

*/

Program to Print Inverted half Triangle (Pyramid) Number Pattern in C

1 2 3 4 
1 2 3
1 2
1
Here is the source code C program to print inverted half triangle using *.
#include<stdio.h>

int main()
{ 
  int i,j; 

  printf("\n");

  for (i=4;i>=1;i--)
  {
	for (j=1;j<=i;j++)
		printf(" %d\t",j);
		printf("\n");
  }

  printf("\n");

  return 0;
}

/* Output of above code:- 

1 2 3 4 
1 2 3
1 2
1

*/

Program to Print Inverted half Triangle (Pyramid) Pattern using Stars (*) in C

* * * * 
* * * 
* * 
* 
Here is the source code C program to print inverted half triangle using *.
#include<stdio.h>

int main()
{ 
  int i,j; 

  printf("\n");

  for (i=4;i>=1;i--)
  {
	for (j=1;j<=i;j++)
		printf("*\t");
		printf("\n");
  }

  printf("\n");

  return 0;
}

/* Output of above code:- 

* * * * 
* * * 
* * 
* 

*/

Program to Print Inverted half Triangle (Pyramid) using Alphabets in C

A B C D 
A B C 
A B 
A 
Here is the source code of Conduct program to print inverted half trainlge using alphabets.
#include <stdio.h>
#include <stdlib.h>

int main()
{ 
  int c,a,e,n,l,num;
 
  printf("\n How many lines:- ");
  scanf("%d",&n); 
  printf("\n");

  if ((n>26) || (n<=0)) 
  {
	printf("\n Error:You can specify only 26 l asines. please specify a numeric value \n \n");
	exit(1);
  }

  for (l=n;l>=1;l--)
  {
	e=64+l;
	for (num=65;num<=e;num++)
		printf("%c\t",num);
		printf("\n");
  }

  printf("\n");L;

  return 0;
}

/* Output of above code:

 How many lines:- 4

A B C D 
A B C 
A B 
A 
*/
Putting too much pattern programs in one post will make it heavy to load so take a lot at other pattern programs that are implemented in C :