You are here

Pattern programs in C

Pattern programs in C language: These programs print various patterns of numbers and stars. These codes illustrate how to create various patterns using C programming. The C programs involve usage of nested for loops (a for loop inside a for loop). A pattern of numbers, star or characters is a way of arranging these in some logical manner or they may form a sequence. Some of these patterns are triangles which have special importance in mathematics. Some patterns are symmetrical while others are not. Please see the complete page and look at comments for many different patterns.

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

We have shown five rows above; in the program, you will be asked to enter the numbers of rows you want to print in the pyramid of stars.

Pattern program in C

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   int row, c, n, s;
  6.  
  7.   printf("Enter the number of rows in pyramid of stars you wish to see\n");
  8.   scanf("%d", &n);
  9.  
  10.   s = n;
  11.  
  12.   for (row = 1; row <= n; row++)  // Loop to print rows
  13.   {
  14.     for (c = 1; c < s; c++)  // Loop to print spaces in a row
  15.       printf(" ");
  16.  
  17.     s--;
  18.  
  19.     for (c = 1; c <= 2*row - 1; c++) // Loop to print stars in a row
  20.       printf("*");
  21.  
  22.     printf("\n");
  23.   }
  24.  
  25.   return 0;
  26. }

Output of program:
Stars pyramid C program output

Download Stars pyramid program.

For more patterns or shapes on numbers and characters see codes on following pages:
Floyd triangle
Pascal triangle

Consider the following triangle pattern
*
**
***
****
*****

to print above pattern see the code below:

Star pattern in C

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   int n, c, k;
  6.  
  7.   printf("Enter number of rows\n");
  8.   scanf("%d", &n);
  9.  
  10.   for (c = 1; c <= n; c++)
  11.   {
  12.     for(k = 1; k <= c; k++)
  13.       printf("*");
  14.  
  15.     printf("\n");
  16.   }
  17.  
  18.   return 0;
  19. }

After understanding these examples, you are in a better position to create your desired pattern. Designing a pattern involves how to use nested loops properly, some patterns may include alphabets or other special characters. A critical aspect is knowing how the characters in pattern change.

C pattern programs

Pattern:

   
   *
  *A*
 *A*A*
*A*A*A*

C pattern program of stars and alphabets:

  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.   int n, c, k, space, count = 1;
  6.  
  7.   printf("Enter number of rows\n");
  8.   scanf("%d", &n);
  9.  
  10.   space = n;
  11.  
  12.   for (c = 1; c <= n; c++)
  13.   {
  14.     for (k = 1; k < space; k++)
  15.       printf(" ");
  16.  
  17.     for (k = 1; k <= c; k++)
  18.     {
  19.       printf("*");
  20.  
  21.       if (c > 1 && count < c)
  22.       {
  23.         printf("A");
  24.         count++;
  25.       }
  26.     }
  27.  
  28.     printf("\n");
  29.     space--;
  30.     count = 1;
  31.   }
  32.   return 0;
  33. }

Pattern:

    1
   232
  34543
 4567654
567898765

C program:

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   int n, c, row, num = 1, space;
  6.  
  7.   scanf("%d", &n);
  8.  
  9.   space = n - 1;
  10.  
  11.   for (row = 1; row <= n; row++)
  12.   {
  13.     num = row;
  14.  
  15.   for (c = 1; c <= space; c++)
  16.     printf(" ");
  17.  
  18.   space--;
  19.  
  20.   for (c = 1; c <= row; c++)
  21.   {
  22.     printf("%d", num);
  23.     num++;
  24.   }
  25.  
  26.   num = num - 2;
  27.  
  28.   for (c = 1 ; c < row; c++)
  29.   {
  30.     printf("%d", num);
  31.     num--;
  32.   }
  33.  
  34.    printf("\n");
  35.   }
  36.  
  37.   return 0;
  38. }