Important Pattern programs in C

In this article, you will find all the pattern programs in C.

pattern programs in c

Pattern programs in C are very important from the exams as well as from the interview point of view. In these programs, we print patterns with the help of code. Pattern programs basically use nested loops, symbols (that you can use to print shape), numbers, and so on. Like in the below examples I had used ‘#‘ and numbers to print patterns.

Pattern Programs In C

Square (With symbol ‘#’)

This is one of the most simple pattern program. Here you need to print a square using a symbol like ‘#’.

#include<stdio.h>

void main() {
  int i, j, n;
  printf("Enter no. of rows in the square: ");
  scanf("%d", & n);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++)
      printf("#");
    printf("\n");
  }

}

OUTPUT

Enter no. of rows in the square: 5
#####
#####
#####
#####
#####

Half pyramid (With Symbol ‘#’)

Here you need to print the shape of a half pyramid using the symbol like ‘#’. In these types of shapes, you need to focus on the lower and upper limits of both the loops.

#include<stdio.h>

void main() {
  int i, j, n;
  printf("Enter the no. of rows: ");
  scanf("%d", & n);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= i; j++)
      printf("#");
    printf("\n");
  }

}

OUTPUT

Enter the no. of rows: 5
#
##
###
####
#####

Full pyramid (With Symbol ‘#’)

Here you need to print the shape of a full pyramid using the symbol like ‘#. Here first you should print the first half of the pyramid by using the approach that you used for half pyramid than the other half.

#include<stdio.h>

void main() {
  int i, j, n, temp;
  printf("Enter the no. of rows in the triangle: ");
  scanf("%d", & n);
  temp = n;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= temp; j++)
      printf(" ");
    temp--;
    for (j = 1; j <= (2 * i) - 1; j++)
      printf("#");
    printf("\n");
  }

}

OUTPUT

Enter the no. of rows in the triangle:  5
     #
    ###
   #####
  #######
 #########

Inverted Full Pyramid (With Symbol ‘#’)

Here you need to print the shape of an inverted full pyramid using the symbol like ‘#. The approach is similar to the one that you used in the full pyramid, only the difference is that you need to start the loop from the upper limit to the lower limit.

#include<stdio.h>

main() {
  int i, j, n, temp;

  printf("Enter the no. of rows in the triangle: ");
  scanf("%d", & n);
  temp = n;
  for (i = n; i >= 1; i--) {
    for (j = temp; j >= 1; j--)

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

}

OUTPUT

Enter the no. of rows in the triangle: 5
     #########
      #######
       #####
        ###
         #

Full Diamond (With Symbol ‘#’)

Here you need to print the shape of a diamond using the symbol like ‘#. You can print a diamond in four parts. In the first two parts, you will print the upper half i.e full pyramid. In the next two half, you will print the inverted pyramid.

#include<stdio.h>

void main() {
  int i, j, n, temp;

  printf("Enter the size of the diamond: ");
  scanf("%d", & n);
  temp = n;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= temp; j++)
      printf(" ");
    temp--;
    for (j = 1; j <= (2 * i) - 1; j++)
      printf("#");
    printf("\n");
  }
  temp = 2;
  for (i = n - 1; i >= 1; i--) {
    for (j = temp; j >= 1; j--)
      printf(" ");
    temp++;
    for (j = 1; j <= (2 * i) - 1; j++)
      printf("#");
    printf("\n");
  }

}

OUTPUT

Enter the size of the diamond: 5
     #
    ###
   #####
  #######
 #########
  #######
   #####
    ###
     #

Full Pyramid of numbers

Here you need to print the shape of a full pyramid using the numbers.

#include<stdio.h>
void main() {
  int n, j, i, k = 1;

  printf("Enter the no. of rows: ");
  scanf("%d", & n);

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

    k = i;

    for (j = 1; j <= i; j++) {
      printf("%d", k);
      k++;
    }

    k = k - 2;

    for (j = 1; j < i; j++) {
      printf("%d", k);
      k--;
    }

    printf("\n");
  }

}

OUTPUT

Enter the no. of rows: 5
    1
   232
  34543
 4567654
567898765

Pascal triangle

Here you need to print the pascal triangle. It is the most important question from the interview point of view.

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

OUTPUT

Enter the number of n: 5
             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1

Floyd’s triangle

Here you need to print the Floyd triangle. The approach is similar to the one used in the half pyramid above, with only the difference that now you have to print the numbers instead of the symbols.

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

OUTPUT

Enter the val of rows: 5
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15

You may also like:-

C Programs

Sorting Algorithms in C

Leave a Comment

Your email address will not be published. Required fields are marked *