Wednesday, August 24, 2011

c program to print patterns, pyramids of numbers and stars



These 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 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 other 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.

C code


#include
#include
 
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();
   return 0;
}

Output:

stars pyramid
Consider the pattern
*
**
***
****
*****
to print above pattern see the code below:
#include
 
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");
    }
 
    return 0;
}

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

Comments


#1 Guest : i need code of this pattern

A B C D E F G H
 A B C D E F G
  A B C D E F 
   A B C D E
    A B C D 
     A B C
      A B
       A

#2 adminPs : c program for character pattern


#include
 
main()
{
      char ch = 'A';
      int n, c, k, space = 0;
 
      scanf("%d", &n);
 
      for ( k = n ; k >= 1 ; k-- )
      {
          for ( c = 1 ; c <= space ; c++)
              printf(" ");
 
          space++;
 
          for ( c = 1 ; c <= k ; c++ )
          {
             printf("%c ", ch); 
             ch++;
          }
 
          printf("\n");
          ch = 'A';
      }
 
      return 0;
}

for this type:

1
01
010
1010
10101

 c program to print pattern


#include
 
main()
{
      int n, c, k, num = 1;
 
      scanf("%d", &n);
 
      for ( c = 1 ; c <= n ; c++ )
      {
          for ( k = 1 ; k <= c ; k++ )
          {
              printf("%d", num);
 
              if ( num == 0 )
                 num = 1;
              else
                 num = 0;
          }
          printf("\n");
      }
 
      return 0;
}

 can you write code for this

p
pr
pro
prog
progr
progra
program

pattern for string

Just input the string and press enter, corresponding pattern will be printed.

#include
#include
 
main()
{
      char string[100];
      int c, k, length;
 
      printf("Enter a string\n");
      gets(string);
 
      length = strlen(string);
 
      for ( c = 0 ; c < length ; c++ )
      {
          for( k = 0 ; k <= c ; k++ )
          {
               printf("%c", string[k]);
          }
          printf("\n");
      }
 
      return 0;
}

 c programming code

please get me the code of the c program to get the output as follows:
1
121
12321
1234321
123454321

 c program to print number pattern


#include
 
main()
{
      int n, c, k, x = 1;
 
      scanf("%d", &n);
 
      for ( c = 1 ; c <= n ; c++ )
      {
          for ( k = 1 ; k <= c ; k++ )
          {
              printf("%d", x);
              x++;
          }
 
          x--;
 
          for ( k = 1 ; k <= c - 1 ; k++ )
          {
              x--;
              printf("%d", x);
          }
 
          printf("\n");
          x = 1;
      }
 
      return 0;
}

 c++ code to print pattern

1   1
12 21
12321

 c++ code to print pattern


#include
 
using namespace std;
 
main()
{
      int n, k, c, space, x, num = 1;
 
      cin >> n;
 
      x = n;
 
      for ( k = 1 ; k <= n ; k++ )
      {
          for ( c = 1 ; c <= k ; c++ )
          {
             cout << num;
             num++;
          }
 
          num--;
 
          for ( c = 1 ; c <= 2*x - 3 ; c++ )
              cout << " ";
 
          x--;
 
          if ( k != n )
          {
             for ( c = 1 ; c <= k  ; c++ )
             {
                cout << num;
                num--;
             }
          }
          else
          {
              num--;
              for ( c = 1 ; c <= k - 1 ; c++ )
              {
                 cout << num;
                 num--;
              }
          }
 
          printf("\n");
          num = 1;
      }    
 
      return 0;
}

 may i get its source code

1
       232
      34543
     4567654
    567898765   

 c code for number pattern


#include
 
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");
 
      }
 
      return 0;
}

how to print this pattern

*******
***S***
**SSS**
*SSSSS*
where S represents Space
*******
*** ***
**   **
*     *

#14 adminPs : c code for star pattern


#include
 
main()
{
    int n, c, k, space, r;
 
    printf("Enter number of rows\n");
    scanf("%d",&n);
 
    space = 1;
    r = n-1;
 
    for( c = 1 ; c <= 2*n - 1 ; c++ )
         printf("*");
 
    printf("\n");
 
    for ( k = 2 ; k <= n ; k++ )
    {
 
        for( c = 1 ; c <= r ; c++ )
            printf("*");
 
        for ( c = 1 ; c <= space ; c++ )
            printf(" ");
 
        space = 2*k-1; 
 
        for( c = 1 ; c <= r ; c++ )
            printf("*");
        r--;
 
        printf("\n");
    }
 
    return 0;
}



In the program, instead of the statement space=2*k-1; , can we go for the statement, space+=2; ?



You can use but don't forget to initialize variable space to one.
 need the code for this
* 
   **
  ***
 ****
*****

 star pattern source code



#include
 
main()
{
    int n, c, k, space;
 
    printf("Enter number of rows\n");
    scanf("%d",&n);
 
    space = n;
 
    for ( k = 1 ; k <= n ; k++ )
    {
        for ( c = 1 ; c < space ; c++ )
            printf(" ");
 
        space--; 
 
        for( c = 1 ; c <= k ; c++ )
            printf("*");
 
        printf("\n");
    }
 
    return 0;
}

need a program to draw the below patern?

1
22
333
4444
55555

#20 adminPs : number pattern source code



#include
 
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("%d", c);
 
        printf("\n");
    }
 
    return 0;
}

need help

i need program to print the following pattern ::
1
2 3
4 5 6
7 8 9 10

Floyd's triangle

This pattern is Floyd's triangle see Floyd's Triangle code.

need a code in c to print below pattern

i want a code to print stars like below
*
**
***
****
***
**
*

 stars pattern using c programming



#include
 
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");
    }
 
    for ( c = n - 2 ; c >= 0 ; c-- )
    {
        for ( k = c ; k >= 0 ; k-- )
            printf("*");
 
        printf("\n");
    }
 
    return 0;
}

GET ME THE CODE OF A PROGRAM

GET ME THE CODE OF A PROGRAM TO GET OUTPUT AS FOLLOWS:
*
  *A*
 *A*A*
*A*A*A*    

 code to print pattern of stars and numbers



#include
 
main()
{
    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");

87 comments:

  1. i want a code to print:

    146
    25
    3

    without goto,using nested loops

    ReplyDelete
    Replies
    1. #include
      Main()
      {
      Int a,b,c,n=146,I;
      a=n%10;
      b=(n/10)%10;
      C=n/100;

      For(I=2;I<=2;I++)
      {
      Printf("/n%d%d",a-b,a-c);
      }
      Printf("/n%d",b-c);
      }

      Delete
    2. I want a code

      a
      bc
      def
      ghij
      klmno

      Frnds help me

      Delete
  2. i want code for
    1
    12
    123
    1234
    by using only one looping statement

    ReplyDelete
    Replies
    1. #include

      void main()
      {
      int r,c
      clrscr();

      for{r=1;r<=4;r++)
      {
      for(c=1;c<=r;c++)
      {
      printf("%d",c);
      }
      printf("\n");
      }
      getch();
      }

      Delete
    2. i want code for reverse a linked list.......

      Delete
  3. hey any one know how to print star pattern in terms of no's.

    ReplyDelete
  4. GET ME THE CODE OF A PROGRAM

    1
    12
    123
    1234
    12345

    ReplyDelete
  5. Give me a simple program with loop for this:
    ____1
    __12 21
    _123 321
    1234 4321

    ReplyDelete
  6. i want to knw the logic behind every progrAM??

    ReplyDelete
  7. please give me the program for getting the output as

    1 2 3 4 5
    16 17 18 19 6
    15 24 25 20 7
    14 23 22 21 8
    13 12 11 10 9

    ReplyDelete
  8. *
    * *
    * *
    * *
    plz give me c program code

    ReplyDelete
  9. please give me the program for output of following pattern
    ****5
    ***4
    **3
    *2
    1

    ReplyDelete
    Replies
    1. #include
      #include
      int main()
      {
      int i,j;
      clrscr();
      for(i=5;i>=1;i--)
      {
      for(j=1;j<=i-1;j++)
      {
      printf("*");
      if(j==i-1)
      printf("%d",i);
      }
      if(i==1)
      printf("%d",i);
      printf("\n");
      }
      getch();
      }

      Delete
  10. hey any 1 help for this program
    1
    3
    5 6
    8 9 10

    ReplyDelete
  11. Give me the codes for following patterns
    (a)
    1
    **
    123
    ****
    12345
    ******
    (b)
    1
    1*
    1*3
    1*3*
    1*3*5
    1*3*5*
    (c)
    1
    *2
    1*3
    *2*4
    1*3*5
    *2*4*6

    Please help me anyone....

    ReplyDelete
  12. hi good blog..
    visit proud2beengineer.com for more complex programs

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. hi pls give me a code that will print this
    5
    4 4
    3 3 3
    2 2 2 2
    1 1 1 1 1

    ReplyDelete
    Replies
    1. for(i=5;i>=1;i--)
      {
      for(j=5;j>=i;j--)
      {
      printf("%d",i);
      }
      printf("\n");
      }

      Delete
  15. *
    *+*
    *++*
    *+++*
    *++++*

    ReplyDelete
  16. i need code for
    10
    98
    765
    4321

    ReplyDelete
  17. 1
    3*2
    4*5*6
    10*9*8*7
    11*12*13*14*15


    tell me the code for this plz

    ReplyDelete
    Replies
    1. #include
      void printPattern(int n)
      {
      int k,i;
      char pattern[20]="",next[20]="";
      k=printPattern(n-1);
      if(n%2==1)
      {
      for(i=1;i<=n;i++)
      {
      k++;
      sprintf(pattern,"%d*",k);
      }
      sprintf(next,"%d",k);
      strcat(pattern,next);
      printf("%s\n",pattern);
      return k;
      }
      else
      {
      beginIndex=k+n;
      lastIndex=k+1;
      for(i=beginIndex;i>lastIndex;i--)
      {
      sprintf(next,"%d*",i);
      strcat(pattern,next);
      }
      sprintf(next,"%d",i);
      strcat(pattern,next);
      printf("%s\n",pattern);
      return beginIndex;
      }
      if(n==1)
      {
      printf("%d",n);
      return 1;
      }
      }

      Delete
  18. *****
    #
    ****
    ##
    ***
    ###
    **
    ####
    *
    #####
    please get me program for dis...

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. i want code for

    1
    2 7
    3 8 12
    4 9 13 16
    5 10 14 17 19
    6 11 15 18 20 21

    ReplyDelete
    Replies
    1. class Pattern1
      {
      public static void main(String[] args)
      {
      int n=6;
      for(int i=0;i<=n-1;i++)
      {
      int a=0;
      a=i+1;
      for(int j=0;j<=i;j++)
      {

      System.out.print(a);
      a=a+n-j-1;
      System.out.print(" ");
      }
      System.out.println();
      }
      }
      }

      Delete
  21. i want to print this pattern
    1
    2 1
    3 2 1
    4 3 2 1
    5 4 3 2 1

    ReplyDelete
    Replies
    1. class Pattern1
      {
      public static void main(String[] args)
      {
      int n=5;
      for(int i=1;i<=n;i++)
      {
      for(int j=i;j>=1;j--)
      {
      System.out.print(j);
      }
      System.out.println();
      }
      }
      }

      Delete
  22. I want to print this
    4
    34
    234
    1234

    ReplyDelete
  23. Write a program to print the following pattern based on user input.
    For eg: If N=4 the code should print the following pattern.
    1*2*3*4*17*18*19*20
    --5*6*7*14*15*16
    ----8*9*12*13
    ------10*11
    Again if N=5 the code should print
    1*2*3*4*5*26*27*28*29*30
    --6*7*8*9*22*23*24*25
    ----10*11*12*19*20*21
    ------13*14*17*18
    --------15*16
    For N=2 the pattern will be
    1*2*5*6
    --3*4
    Do not use blank spaces instead of dashes.
    I could never frame the code even after lot of trying.Please help.

    ReplyDelete
  24. Write a program to print a swastic

    ReplyDelete
  25. Write a program to print tha digit in shortest form to a number?

    ReplyDelete
  26. i want a code for
    A
    A B
    A B C
    A B C D

    ReplyDelete
  27. i want for
    *******
    *** ***
    ** **
    * *
    ** **
    *** ***
    *******

    ReplyDelete
  28. PRINTING THE PATTERNS
    A
    A B
    A B C
    A B C D
    A B C D E

    ReplyDelete
  29. Print this pattern in C please (not c++)
    * *
    * *
    * *
    * *
    * *
    * *
    * *

    ReplyDelete
  30. This comment has been removed by the author.

    ReplyDelete
  31. i want this type of code
    12345
    6 7 8
    10 11 12
    13 14 15 16 17

    ReplyDelete
  32. 1 2 3 4 5
    6 7 8
    10 9 12
    13 14 15 16 17

    ReplyDelete
  33. write a program to print
    1 2 3 4 5
    2 3 4 5
    3 4 5
    4 5
    5

    ReplyDelete
  34. Replies
    1. #include
      int main()
      {
      int num,r,c;
      static int i=1;
      static char ch='A';
      printf("Enter no. of rows : ");
      scanf("%d", &num);
      for(r=1; r<=num; r++)
      {
      for(c=1; c<=r; c++)
      {
      if(r%2==0)
      printf(" %c",ch++);
      else
      printf(" %d",i++);
      }
      printf("\n");
      }
      getch();
      return 0;
      }

      Delete
  35. A D G J M
    19 17 13 11
    P S V
    7 5
    Y

    ReplyDelete
  36. A D G J M
    19 17 13 11
    P S V
    7 5
    Y

    ReplyDelete
  37. A D G J M
    19 17 13 11
    P S V
    7 5
    Y

    ReplyDelete
  38. 1
    2 1 2
    3 2 1 2 3
    4 3 2 1 2 3 4

    ReplyDelete
  39. C++ Program to Print Triangle of Stars

    Print triangle of stars is depend of two concept, first you need outer loop for line break and inner loop for increment and decrements.

    ReplyDelete
  40. C++ Program to Print number pattern

    In c++ we can easily print any number patter just we need two for loop one inner loop for increment and decrements and other for line break.

    ReplyDelete
  41. plece give me code to print this
    *
    * *
    * * *
    * * * *
    * * * * *

    ReplyDelete
  42. can we write a program for tringular pattern with only 1 for loop

    ReplyDelete
  43. pplace give me code to print this
    1
    2 1 2
    3 2 1 2 3
    4 3 2 1 2 3 4

    ReplyDelete
  44. please give me the program for output of following pattern
    ****4
    ***3
    **2
    *1

    ReplyDelete
  45. Give a program for
    1
    1 2
    1 2 3
    1 2 3 4

    ReplyDelete
  46. Give a program for
    1
    1 2
    1 2 3
    1 2 3 4

    ReplyDelete
  47. send me the programme to print pattern M

    ReplyDelete
  48. hi pliz gv me a code that wl print
    1
    3 1
    5 3 1
    7 5 3 1
    9 5 7 3 1

    ReplyDelete
  49. I want c program for this output:

    ABCDDCBA
    ABC CBA
    AB BA
    A A

    ReplyDelete
  50. i want code for this output:
    333
    313
    323
    333

    ReplyDelete
  51. can i get
    1 2 3 4 5
    6 7 8 9 10
    11 12 13 14 15
    16 17 18 19 20
    21 22 23 24 25

    ReplyDelete
  52. can i get
    1 2 3 4 5
    6 7 8 9 10
    11 12 13 14 15
    16 17 18 19 20
    21 22 23 24 25

    ReplyDelete
  53. Very informative article.Thank you author for posting this kind of article .


    http://www.wikitechy.com/view-article/java-program-to-print-floyds-triangle-example



    Both are really good,
    Cheers,
    Venkat

    ReplyDelete
  54. can some one plz print the output as
    123456789
    13579
    157
    17
    7

    ReplyDelete
  55. 1
    A b
    3 4 5
    c d e f
    Plz anyone help me

    ReplyDelete
  56. 1
    01
    101
    0101
    10101
    plz anyone help me

    ReplyDelete