Pattern printing program in C

Here, we  will make some program for pattern printing in C language. There are various types of pattern to print in any programming language. We will try to cover most of their concept to print pattern. So, let’s see some C program for pattern printing.

It is very important to know the concept of nested loop to print pattern in C. We will print some number pattern, alphabetic pattern as well as star pattern here.

Program for Pattern printing

Here we will write a C program which will print the following pattern.

Enter the number of row here : 8
       *
      ***
     *****
    *******
   *********
  ***********
 *************
***************

Now, let’s see the program to print pattern.

// pattern program in c
#include <stdio.h>
int main(){
  int num, x, row;

  printf("Enter the number of row here : ");
  scanf("%d", &num);

  for (row = 1; row <= num; row++){

    for (x = 1; x <= num-row; x++){
        printf(" ");
    }

    for (x = 1; x <= 2*row - 1; x++){
      printf("*");
    }

    printf("\n");
  }

  return 0;
}

Output of this pattern printing program:

pattern printing program in c

Another pattern program in C

// pattern program using c
#include <stdio.h>
int main(){
  int num, j, k;
  printf("Enter number the number of rows here : ");
  scanf("%d", &num);

  for (j = 1; j <= num; j++){
    for(k = 1; k <= j; k++){
      printf("*");
    }
    printf("\n");
  }

  return 0;
}

Output of C pattern program:

pattern program in c

Diamond pattern program by stars in C

// diamond pattern program by stars using c
#include <stdio.h>
int main(){
  int i, j, num;
  printf("Enter number of row to print diamond : ");
  scanf("%d", &num);

  for (j = 1; j <= num; j++){

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

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

    printf("\n");
  }

  for (j = 1; j <= num - 1; j++){

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

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

    printf("\n");
  }

  return 0;
}

Output of diamond pattern program:

diamond pattern printing program in c

Number pattern program in C

// number pattern program in c
#include <stdio.h>
int main(){
  int num, row, i, total = 1;
  printf("Enter the number of row : ");
  scanf("%d", &num);

  for (row = 1; row <= num; row++){
    for (i = 1; i <= num - row; i++){
      printf(" ");
    }

    total = row;

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

    total = total - 2;

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

    printf("\n");
  }

  return 0;
}

Output of number pattern program:

print number pattern in c

Matrix pattern program in C

// matrix pattern program in c
#include <stdio.h>
int main(){
  int num, row, i, j, m, p = 1, arr[100][100];
  printf("Enter the number of rows in matrix : ");
  scanf("%d", &row);

  for (i = 1; i <= row; i++){
    m = 0;
    num = i;

    for (j = 1; j <= i; j++){
      arr[m++][--num] = p++;
    }
  }

  for (i = 1; i <= row-1; i++){
    m = i;
    num = row-1;

    for (j = 1; j<= row-i; j++){
      arr[m++][num--] = p++;
    }
  }

  for (i = 0; i <= row-1; i++){

    for (j = 0; j <= row-1; j++){
      printf("%d ", arr[i][j]);
    }

    printf("\n");
  }

  return 0;
}

Output of matrix pattern:

If you understand every pattern printing program which we have given here, then it will be very easy to print any pattern using C for you. So, if you don’t have clear idea about them, then we recommend you to try these programs again. You can also make some similar pattern program using C.

Previous pagenCr and nPr in C

Next pageFloyd’s triangle by C