Home C Examples 10+ C Program to Print Patterns

10+ C Program to Print Patterns

by Prateek Kashyap
6 minutes read

In this example, you’ll learn how to print patterns using C program such as half pyramids of numbers, inverted pyramids numbers, Inverted Half pyramid of Numbers, Hollow Half Pyramid of Numbers, Full Pyramid of Numbers, Hollow Full Pyramid of Numbers, Hollow Inverted Half Pyramid of Numbers in C Programming.

What is pattern in C?

Pattern programs in C language, showing how to create various patterns of numbers and stars. The programs require nested loops (a loop inside another loop). A design of numerals, stars, or characters is a way of arranging these in some logical manner, or they may form a sequence.

To understand C Program to Print Patterns, you should have knowledge of the following C programming topics:

  • C if…else Statement
  • C for Loop
  • C while and do…while Loop
  • C break and continue

Half Pyramid of Numbers

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

C Program To Print Half Pyramid of Numbers

#include<stdio.h>
int main()
{
     int rows;
     printf("Enter the number of rows:");
     scanf("%d",&rows);
    for(int i=1;i<=rows;i++)
   {
   	for(int j=1;j<=i;j++)
   	{
   	printf("%d ",j);
	   }
	   printf("\n");
   }
   
 return 0;
}

Inverted Half Pyramid of Numbers

1 2 3 4 5
1 2 3 4
1 2 3
1 2 
1

C Program to Print Inverted Half Pyramid of Numbers

#include<stdio.h>

int main()
{
   int rows;
   printf("Enter the numbers of rows:");
   scanf("%d",&rows);
   for(int i=rows;i>=1;i--)
   {
   	for(int j=1;j<=i;j++)
   	{
   		printf("%d ",j);
	   }
	   printf("\n");
   }
   
 return 0;
}

Hollow Half Pyramid of Numbers

1    
1 2   
1   3  
1     4
1 2 3 4 5

C Program To Print Hollow Half Pyramid of Numbers

#include<stdio.h>

int main()
{
   int rows;
   printf("Enter the number of rows:");
   scanf("%d",&rows);
   for(int i=1;i<=rows;i++)
   {
   	
    for(int j=1;j<=i;j++)
   	{   if(i==rows)
   	      {
   	    	printf("%d",j);
	      }
	    else
		{
	       if(j==1||j==i)
   	       printf("%d",j);
   	       else
   	       printf(" ");
       
         }
       }
	   printf("\n");
   }
   
return 0;
}

Full Pyramid of Numbers

        1
      2 3 2
    3 4 5 4 3
  4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5      

C Program to Print Full Pyramid of Numbers

#include<stdio.h>

int main()
{
    int rows;
    printf("Enter the number of rows:");
    scanf("%d",&rows);
   for(int i=1;i<=rows;i++)
   {
   	for(int z=rows;z>i;z--)
   	{
         printf(" ");
        }
   	           
       for(int j=i;j<=i*2-1;j++)
   	{
         printf("%d",j);
        }
	           
        for(int z=(i-1)*2;z>=i;z--)
	{
         printf("%d",z);
	}
	
         printf("\n");
   
}
 
return 0;
}

Hollow Full Pyramid of Numbers

        1
      1   2
    1       3
  1           4
1   2   3   4   5    

C Program To Print Hollow Full Pyramid of Numbers

#include<stdio.h>
int main()
{
   int rows;
   int z=1,q=1;
   printf("Enter the number of rows:");
   scanf("%d",&rows);
   for(int i=1;i<=rows;i++)
   {
   	for(int z=rows;z>i;z--)
   	{
              printf(" ");
        }
        for(int j=1;j<=i*2-1;j++)
   	{    
        if(i==rows)
   	{  
   	   if (j%2==0)
   	   {
	      printf(" ");
   	      z++;
   	   }
   	   else
   	       printf("%d",z);
   	}
	else
	{
   	  if(j==1)
          {
	   printf("%d",j);
	  }
	  else if(j==i*2-1)
	  {
            printf("%d",q+1);
	    q++;
	   }
          else
          {
	    printf(" ");
          }
        }
   }
	   
printf("\n");
   }
 
return 0;
}

Hollow Inverted Half Pyramid of Numbers

1 2 3 4 5
2     5
3   5
4 5 
5

C Program To Print Hollow Inverted Half Pyramid of Numbers

#include<stdio.h>

int main()
{
   int n;
   printf("Enter the value of N:");
   scanf("%d",&n);
   for(int i=1;i<=n;i++)
   {
   	for(int j=i;j<=n;j++)
   	{   
            if (i==1)
   	    {
   	    	printf("%d",j);
	    }
	    else
		{
   	         if(j==i||j==n)
   			printf("%d",j);
   		 else
   			printf(" ");
	        }
	}  
	   printf("\n");
   }
   return 0;
}

What is Floyd’s Triangle?

Floyd’s triangle is a series of numbers that are sequentially spread across a series of rows. The first row contains a 1 by itself, and the second row contains 2 and 3. The next row holds 4, 5, and 6, and the numbers continue in this pattern infinitely.

Floyd’s Triangle

1
2 3
4 5 6
7 8 9 10

C Program To Print Floyd’s Triangle

#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;
}

What is Pascal’s Triangle?

C Program To Print Floyd's Triangle

Pascal’s Triangle is a triangle of numbers where each number is the two numbers directly above it added together (except for the edges, which are all “1”).

Pascal’s Triangle

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

C Program To Print Floyd’s Triangle

#include <stdio.h>
int main() {
   int rows, coef = 1, space, i, j;
   printf("Enter the 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)
            coef = 1;
         else
            coef = coef * (i - j + 1) / j;
         printf("%4d", coef);
      }
      printf("\n");
   }
   return 0;
}

Are pattern prints asked in interviews?

Coding interviews often tend to ask a print patterns program to test the candidates. Usually, in an interview process lasting four rounds, the first round tends to be one programming round, and there is a probability that one of the questions asked could be a pattern program.

Hope this article on C Program to Print Patterns and Pyramids is useful to you. Don’t forget to share!

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.