This program prints various different patterns of numbers and stars. These codes illustrate how to create various patterns using c programming. Most of these c programs involve usage of nested loops and space. A pattern of numbers, star,s 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 the 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.


C programming code

Pattern numbers stars stables
Programming codes for the above patterns are given below and you can change them according to your desire.

#include <stdio.h>
#include <conio.h>
int main()
{
    int n, c, k;
    printf("Enter number of rows\n");
    scanf("%d",&n);
    for ( c = 1 ; c <= n ; c++ )
    {
        for( k = 1 ; k <= c ; k++ )
            printf("*");
 
        printf("\n");
    }
 
    getch();
}

 Example No 2
The following pattern is given below and we have to write its code and program will ask you how many rows of pattern you want to print
*
***
*****
*******
*********
Because there are 5 rows of the above program so we will give 5 rows to the program. The code of pattern is given below


#include <stdio.h>
#include <coni0.h>
void main()
{
   int row, c, n, temp;
   printf("Enter the number of rows in pyramid of stars you wish to see ");
   scanf("%d",&n);
   temp = n;
   for ( row = 1 ; row <= n ; row++ )
   {
      for ( c = 1 ; c < temp ; c++ )
         printf(" ");
 
      temp--;
 
      for ( c = 1 ; c <= 2*row - 1 ; c++ )
         printf("*");
 
      printf("\n");
   } 
   getch();
}

The output of the above code is

Using these examples you are in a better position to create your desired pattern for yourself. Creating a pattern involves how to use nested loops properly, some patterns may involve alphabets or other special characters. A key aspect is knowing how the characters in the pattern change.


C pattern programs example 3

Pattern:

   #
  #A#
 #A#A#
#A#A#A#

C pattern program of hash and A:

#include<stdio.h>
#include<stdio.h>
void main()
{
clrscr();
    int n, c, k, space, count = 1;
    printf("Enter number of rows\n");
    scanf("%d",&n);
    space = n;
    for ( c = 1 ; c <= n ; c++)
    {
        for( k = 1 ; k < space ; k++)
           printf(" ");
 
        for ( k = 1 ; k <= c ; k++)
        {
            printf("#");
 
            if ( c > 1 && count < c)
            {
                 printf("A");
                 count++;
            }
        }
        printf("\n");
        space--;
        count = 1;
    }
    getch();
}

 

Pattern:

        1
       232
      34543
     4567654
    567898765

C program:

#include<stdio.h>
#include<stdio.h>
void main()
{
      int n, c, d, num = 1, space;
      scanf("%d",&n);
      space = n - 1;
      for ( d = 1 ; d <= n ; d++ )
      {
          num = d;
          for ( c = 1 ; c <= space ; c++ )
              printf(" ");
          space--;
 
          for ( c = 1 ; c <= d ; c++ )
          {
              printf("%d", num);
              num++;
          }
          num--;
          num--;
          for ( c = 1 ; c < d ; c++)
          {
              printf("%d", num);
              num--;
          }
          printf("\n");
 
      }
 
      getch();
}

I hope you will like these examples of patterns if you don’t understand these examples clearly visits my videos lectures with full explanations on youtube. ch-anal name is Gillani data

Advertisements
Advertisements
Advertisements