10 Challenging star pattern programs in C

by Utsav Banerjee on October 19, 2009


star pattern screenshotComputer languages are not as easy as human languages, they need you to become a computer yourself, think like a computer and write an algorithm. Computer programs are fun if you take them up as a challenge, as unsolved puzzles. There is lot of fun in taking up the challenge, understanding the problem, writing an algorithm, writing a program and most importantly running the program and obtaining required output.

Here are few challenging C program questions for you. Let’s see how many of them you would be able to write without seeing the program solution given below. These questions are to print patterns using asterisk(star) character. Comment below if you have tougher pattern questions.

Also read Number Pattern Programs

  1. Write a C program to print the following pattern:
             *
          *  *
       *  *  *
    *  *  *  *
    
  2. Write a C program to print the following pattern:
    *                   *
    * * *           * * *
    * * * * *   * * * * *
    * * * * * * * * * * *
    
  3. Write a C program to print the following pattern:
    *                         *
       *                   *
    *     *             *     *
       *     *       *     *
    *     *      *      *     *
       *     *       *     *
    *     *             *     *
       *                   *
    *                         *
    
  4. Write a C program to print the following pattern:
    *   *   *   *   *
      *   *   *   *
        *   *   *
          *   *
            *
          *   *
        *   *   *
      *   *   *   *
    *   *   *   *   *
    
  5. Write a C program to print the following pattern:
              *
            * * *
          * * * * *
        * * * * * * *
      * * * * * * * * *
        * * * * * * *
          * * * * *
            * * *
              *
            * * *
          * * * * *
    
  6. Write a C program to print the following pattern:
         *
           * *
             * * *
               * * * *
             * * *
           * *
         *
    
  7. Write a C program to print the following pattern:
    * * * * * * * * *
    * * * *   * * * *
    * * *       * * *
    * *           * *
    *               *
    * *           * *
    * * *       * * *
    * * * *   * * * *
    * * * * * * * * *
    
  8. Write a C program to print the following pattern:
    * * * * * * * * * * * * * * * * *
      * * * * * * *   * * * * * * *
        * * * * *       * * * * *
          * * *           * * *
            * * * * * * * * *
              * * * * * * *
                * * * * *
                  * * *
                    *
    
  9. Write a C program to print the following pattern:
          *
        * * *
      * * * * *
    * * * * * * *
    *           *
    * *       * *
    * * *   * * *
    * * * * * * *
    * * *   * * *
    * *       * *
    *           *
    * * * * * * *
      * * * * *
        * * *
          *
    
  10. Write a C program to print the following pattern:
    * * * * * * * * * * * * * * * * * * * * * * * * *
      *           *   *           *   *           *
        *       *       *       *       *       *
          *   *           *   *           *   *
            *               *               *
          *   *           *   *           *   *
        *       *       *       *       *       *
      *           *   *           *   *           *
    * * * * * * * * * * * * * * * * * * * * * * * * *
    
     

  1. Write a C program to print the following pattern:
  2.          *
          *  *
       *  *  *
    *  *  *  *

    Program:

    /* This is a simple mirror-image of a right angle triangle */
    
    #include
    int main() {
     char prnt = '*';
     int i, j, nos = 4, s;
     for (i = 1; i <= 5; i++) { for (s = nos; s >= 1; s--) {  // Spacing factor
       printf("  ");
      }
      for (j = 1; j <= i; j++) {
       printf("%2c", prnt);
      }
      printf("\n");
      --nos;   // Controls the spacing factor
     }
     return 0;
    }

    Download Code

    Back to top

  3. Write C program to print the following pattern:
    *                   *
    * * *           * * *
    * * * * *   * * * * *
    * * * * * * * * * * *
  4. Program:

    #include
    int main() {
     char prnt = '*';
     int i, j, k, s, c = 1, nos = 9;
     for (i = 1; c <= 4; i++) {
         // As we want to print the columns in odd sequence viz. 1,3,5,.etc
      if ((i % 2) != 0) {
       for (j = 1; j <= i; j++) {  printf("%2c", prnt); } for (s = nos; s >= 1; s--) { //The spacing factor
        if (c == 4 && s == 1) {
         break;
        }
        printf("  ");
       }
       for (k = 1; k <= i; k++) {
        if (c == 4 && k == 5) {
         break;
        }
        printf("%2c", prnt);
       }
       printf("\n");
       nos = nos - 4;  // controls the spacing factor
       ++c;
      }
     }
     return 0;
    }

    Download Code

    Back to top

  5. Write C program to print the following pattern:
    *                         *
       *                   *
    *     *             *     *
       *     *       *     *
    *     *      *      *     *
       *     *       *     *
    *     *             *     *
       *                   *
    *                         *
  6. Program:

    #include
    int main() {
     char prnt = '*';
     int i, j, k, s, p, r, nos = 7;
    
     for (i = 1; i <= 5; i++) {
      for (j = 1; j <= i; j++) {  if ((i % 2) != 0 && (j % 2) != 0) { printf("%3c", prnt); } else if ((i % 2) == 0 && (j % 2) == 0) { printf("%3c", prnt); } else { printf("   "); } } for (s = nos; s >= 1; s--) {  // for the spacing factor
       printf("   ");
      }
      for (k = 1; k <= i; k++) { //Joining seperate figures if (i == 5 && k == 1) {  continue; } if ((k % 2) != 0) { printf("%3c", prnt); } else { printf("   "); } } printf("\n"); nos = nos - 2;   // space control }  nos = 1;  // remaining half.. for (p = 4; p >= 1; p--) {
      for (r = 1; r <= p; r++) { if ((p % 2) != 0 && (r % 2) != 0) { printf("%3c", prnt); } else if ((p % 2) == 0 && (r % 2) == 0) { printf("%3c", prnt); } else { printf("   "); } } for (s = nos; s >= 1; s--) {
       printf("   ");
      }
      for (k = 1; k <= p; k++) {
       if ((k % 2) != 0) {
        printf("%3c", prnt);
       }
    else {
        printf("   ");
       }
      }
      nos = nos + 2;  // space control
      printf("\n");
     }
     return 0;
    }

    Download Code

    Explanation:
    This can be seen as an inverted diamond composed of stars. It can be noted that the composition of this figure follows sequential pattern of consecutive stars and spaces.

    In case of odd row number, the odd column positions will be filled up with ‘*’, else a space will be spaced and vice-versa in case of even numbered row.

    In order to achieve this we will construct four different right angle triangles

    aligned as per the requirement.
    Back to top

  7. Write a C program to print the following pattern:
    *   *   *   *   *
      *   *   *   *
        *   *   *
          *   *
            *
          *   *
        *   *   *
      *   *   *   *
    *   *   *   *   *
  8. Program:

    #include
    int main() {
     char prnt = '*';
     int i, j, s, nos = 0;
     for (i = 9; i >= 1; (i = i - 2)) {
      for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (j = 1; j <= i; j++) {
       if ((i % 2) != 0 && (j % 2) != 0) {
        printf("%2c", prnt);
       } else {
        printf("  ");
       }
      }
      printf("\n");
      nos++;
     }
     nos = 3;
     for (i = 3; i <= 9; (i = i + 2)) { for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (j = 1; j <= i; j++) {
    
       if ((i % 2) != 0 && (j % 2) != 0) {
        printf("%2c", prnt);
       } else {
        printf("  ");
       }
      }
      nos--;
      printf("\n");
     }
     return 0;
    }

    Download Code
    Back to top

  9. Write a C program to print the following pattern:
              *
            * * *
          * * * * *
        * * * * * * *
      * * * * * * * * *
        * * * * * * *
          * * * * *
            * * *
              *
            * * *
          * * * * *
  10. Program:

    #include
    int main() {
     char prnt = '*';
     int i, j, k, s, nos = 4;
     for (i = 1; i <= 5; i++) { for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (j = 1; j <= i; j++) {
       printf("%2c", prnt);
      }
      for (k = 1; k <= (i - 1); k++) { if (i == 1) {     continue; } printf("%2c", prnt); }  printf("\n");   nos--; }  nos = 1; for (i = 4; i >= 1; i--) {
      for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (j = 1; j <= i; j++) {
       printf("%2c", prnt);
      }
      for (k = 1; k <= (i - 1); k++) {
       printf("%2c", prnt);
      }
      nos++;
      printf("\n");
     }
     nos = 3;
     for (i = 2; i <= 5; i++) { if ((i % 2) != 0) { for (s = nos; s >= 1; s--) {
        printf("  ");
       }
       for (j = 1; j <= i; j++) {
        printf("%2c", prnt);
       }
      }
      if ((i % 2) != 0) {
       printf("\n");
       nos--;
      }
     }
     return 0;
    }

    Download Code
    Back to top

  11. Write a C program to print the following pattern:
         *
           * *
             * * *
               * * * *
             * * *
           * *
         *
  12. Program:

    /*
        This can be seen as two right angle triangles sharing the same base
        which is modified by adding few extra shifting spaces
    */
    #include
    // This function controls the inner loop and the spacing
    // factor guided by the outer loop index and the spacing index.
    int triangle(int nos, int i) {
     char prnt = '*';
     int s, j;
     for (s = nos; s >= 1; s--) {    // Spacing factor
      printf("  ");
     }
     for (j = 1; j <= i; j++) {      //The inner loop
      printf("%2c", prnt);
     }
     return 0;
    }
    
    int main() {
     int i, nos = 5;
     //draws the upper triangle
     for (i = 1; i <= 4; i++) {  triangle(nos, i);    //Inner loop construction  nos++;              // Increments the spacing factor  printf("\n");  } nos = 7;  //Draws the lower triangle skipping its base. for (i = 3; i >= 1; i--) {
      int j = 1;
      triangle(nos, i);  // Inner loop construction
      nos = nos - j;     // Spacing factor
      printf("\n");
     }
     return 0;
    }

    Download Code
    Back to top

  13. Write a C program to print the following pattern:
    * * * * * * * * *
    * * * *   * * * *
    * * *       * * *
    * *           * *
    *               *
    * *           * *
    * * *       * * *
    * * * *   * * * *
    * * * * * * * * *
  14. Program:

    #include
    
    int main() {
     char prnt = '*';
     int i, j, k, s, nos = -1;
     for (i = 5; i >= 1; i--) {
      for (j = 1; j <= i; j++) {  printf("%2c", prnt); } for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (k = 1; k <= i; k++) {
       if (i == 5 && k == 5) {
        continue;
       }
       printf("%2c", prnt);
      }
      nos = nos + 2;
      printf("\n");
     }
     nos = 5;
     for (i = 2; i <= 5; i++) {
      for (j = 1; j <= i; j++) {  printf("%2c", prnt); } for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (k = 1; k <= i; k++) {
       if (i == 5 && k == 5) {
        break;
       }
       printf("%2c", prnt);
      }
      nos = nos - 2;
      printf("\n");
     }
     return 0;
    }

    Download Code
    Back to top

  15. Write a C program to print the following pattern:
    * * * * * * * * * * * * * * * * *
      * * * * * * *   * * * * * * *
        * * * * *       * * * * *
          * * *           * * *
            * * * * * * * * *
              * * * * * * *
                * * * * *
                  * * *
                    *
  16. Program:

    #include
    int main() {
     char prnt = '*';
     int i, j, k, s, sp, nos = 0, nosp = -1;
     for (i = 9; i >= 3; (i = i - 2)) {
      for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (j = 1; j <= i; j++) { printf("%2c", prnt); } for (sp = nosp; sp >= 1; sp--) {
       printf("  ");
      }
      for (k = 1; k <= i; k++) {  if (i == 9 && k == 1) { continue; } printf("%2c", prnt); } nos++; nosp = nosp + 2; printf("\n"); } nos = 4; for (i = 9; i >= 1; (i = i - 2)) {
      for (s = nos; s >= 1; s--) {
       printf("  ");
      }
      for (j = 1; j <= i; j++) {
       printf("%2c", prnt);
      }
      nos++;
      printf("\n");
     }
    
     return 0;
    }

    Download Code
    Back to top

  17. Write a C program to print the following pattern:
          *
        * * *
      * * * * *
    * * * * * * *
    *           *
    * *       * *
    * * *   * * *
    * * * * * * *
    * * *   * * *
    * *       * *
    *           *
    * * * * * * *
      * * * * *
        * * *
          *
  18. Program:

    #include
    /*
     * nos = Num. of spaces required in the triangle.
     * i   = Counter for the num. of charcters to print in each row
     * skip= A flag for checking whether to
     *       skip a character in a row.
     *
     */
    int triangle(int nos, int i, int skip) {
     char prnt = '*';
     int s, j;
     for (s = nos; s >= 1; s--) {
      printf("  ");
     }
     for (j = 1; j <= i; j++) {
      if (skip != 0) {
       if (i == 4 && j == 1) {
        continue;
       }
      }
      printf("%2c", prnt);
     }
     return 0;
    }
    
    int main() {
     int i, nos = 4;
     for (i = 1; i <= 7; (i = i + 2)) {
      triangle(nos, i, 0);
      nos--;
      printf("\n");
     }
     nos = 5;
     for (i = 1; i <= 4; i++) { triangle(1, i, 0); //one space needed in each case of the formation triangle(nos, i, 1); //skip printing one star in the last row. nos = nos - 2; printf("\n"); } nos = 1; for (i = 3; i >= 1; i--) {
      triangle(1, i, 0);
      triangle(nos, i, 0);
      nos = nos + 2;
      printf("\n");
     }
     nos = 1;
     for (i = 7; i >= 1; (i = i - 2)) {
      triangle(nos, i, 0);
      nos++;
      printf("\n");
     }
     return 0;
    }

    Download Code
    Back to top

  19. Write a C program to print the following pattern:
    * * * * * * * * * * * * * * * * * * * * * * * * *
      *           *   *           *   *           *
        *       *       *       *       *       *
          *   *           *   *           *   *
            *               *               *
          *   *           *   *           *   *
        *       *       *       *       *       *
      *           *   *           *   *           *
    * * * * * * * * * * * * * * * * * * * * * * * * *
  20. Program:

    #include
    
    /*
     * nos = Num. of spaces required in the triangle.
     * i   = Counter for the num. of characters to print in each row
     * skip= A flag for check whether to
     *       skip a character in a row.
     *
     */
    
    int triangle(int nos, int i, int skip) {
     char prnt = '*';
     int s, j;
     for (s = nos; s >= 1; s--) {
      printf("  ");
     }
     for (j = 1; j <= i; j++) {  if (skip != 0) {  if (i == 9 && j == 1) {    continue;  }  } if (i == 1 || i == 9) {  printf("%2c", prnt); } else if (j == 1 || j == i) {  printf("%2c", prnt);  } else {  printf("  "); }  } return 0; } int main() { int i, nos = 0, nosp = -1, nbsp = -1; for (i = 9; i >= 1; (i = i - 2)) {
      triangle(nos, i, 0);
      triangle(nosp, i, 1);
      triangle(nbsp, i, 1);
      printf("\n");
      nos++;
      nosp = nosp + 2;
      nbsp = nbsp + 2;
     }
     nos = 3, nosp = 5, nbsp = 5;
     for (i = 3; i <= 9; (i = i + 2)) {
      triangle(nos, i, 0);
      triangle(nosp, i, 1);
      triangle(nbsp, i, 1);
      printf("\n");
      nos--;
      nosp = nosp - 2;
      nbsp = nbsp - 2;
     }
     return 0;
    }

    Download Code
    Back to top

Over 1600 professionals follow Interview Mantra.


About the Author:  This post was written by Utsav Banerjee. You can reach Utsav on email at fugitiveland@gmail.com


2,250 readers have already subscribed to email updates!

Enter your email address:

  • Anonymous

    thats good job keep it up

  • http://www.interviewmantra.net sridhar

    The credit of this post goes to Utsav Banerjee.. Good work Utsav!
    Editor,
    Interview Mantra

  • Anonymous

    the examples r very well choosen nd explained……….great job…….i too was searching for the answers for a long time……nd i got my solution here………..thnx a lot……….

  • Shyamala Vydyam

    Ultimate Examples !!! Looks very professional your problem solving !!! Great & Great!!

  • admin

    Thanks Shyamala. Glad that you liked our examples.

  • abhang savita

    thanks …..i think it is good for improving my knowledge

  • Nipun

    Hay many many thanks for give a nice shape of star

  • harpreet

    plz tell me code for this pattern
    1
    2 6
    3 7 10
    4 8 11 13
    5 9 12 14 15

  • Devender Mishra

    @ Harpreet
    Here is the code for your pattern.

    Code 1: # let’s we have enter the number of line say 5 here

    int i,j,pval;
    for(i=1;i<=n;i++)
    {
    printf("%d ",i);pval=i;
    for(j=2;j<=i;j++){pval+=n-j+1; cout<<pval<<" ";}
    cout<<"\n";
    }

    Code 2: # Let's we have number N, maximum value to be printed

    int i,j;
    for(i=1;i*i<=8*N+1;i+=2);
    int n,pval;
    n=(i-2)/2;
    n = n*(n+1)/2==N?n:n+1;
    for(i=1;i<=n;i++)
    {
    (i<=N)&&printf("%d ",i)||1; pval=i;
    for(j=2;j<=i;j++){pval+=n-j+1;(pval<=N)&&printf("%d ",pval);}
    cout<<"\n";
    }

  • henu

    plz send me code for the pattern
    1
    23
    456

  • derek

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

  • derek

    sorry not with the space in from of the first asterisks, i have an assignment and i understand loops alright i just need help with the conditions i get myself confussed lol. . and its a C programming class so nothing from c++

  • http://interviewmantra.com shikha srivastava

    thanuuuuuuuuuuuuu sooooooooo much ..ossam

  • http://www.sahilaggarwalgju.blogspot.com Sahil

    tell me the code of this
    1
    2 3
    4 5 6
    7 8 9 10

  • ranjan

    can u give me the code for a star moving in a circle or square…it must be moving..and should give a beep sound when its moving..! like a single * in a circle with sound..!
    **** * * * * * * * *
    * * * *
    * * .. or * *
    * * * *
    * * * * * * * * * *

  • Utsav Banerjee

    As the author of the post , I would like to request everyone not to ask for ready made codes as I am strictly against spoon feeding. Please try out the problem yourself post your code in pastebin.com n refer us the link so that we can verify your code, and help you solve the riddle.

    Thanking You
    Utsav Banerjee

  • mathapelo

    8 Write a separate algorithm for a program to output each of the following patterns
    [Note: There are four triangles of stars labelled (a) (b) (c) (d). Each triangle consists of ten lines of stars. Starting from line 1 to 10, the number of stars
    either increases by 1 from 1 to 10
    or decreases by 1 from 10 to 1]:

    (a) (b) (c) (d)
    * ********** ********** *
    ** ********* ********* **
    *** ******** ******** ***
    **** ******* ******* ****
    ***** ****** ****** *****
    ****** ***** ***** ******
    ******* **** **** *******
    ******** *** *** ********
    ********* ** ** *********
    ********** * * **********

  • Pramod

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

    How to print this pattern

  • Pramod

    *
    * *
    * *
    * * * * * *

  • kavita

    plz help me to write dis pattern
    1 2 3 4 5 4 3 2 1
    1 2 3 4 4 3 2 1
    1 2 3 3 2 1
    1 2 2 1
    1 1

  • kavita

    1 2 3 4 5 4 3 2 1
    1 2 3 4 4 3 2 1
    1 2 3 3 2 1
    1 2 2 1
    1 1

  • sneha

    plz help me to write ds pattern!!
    *****
    *****
    *****
    *****
    *****

  • pratik

    plz help mi to write ths pattern program..
    *****************
    * *
    * *
    ****** ******
    1 * *
    * *
    **** ****
    * *
    ***********

  • sushant

    plz help mi to write ths pattern program..
    *
    * * *
    * * * * *
    * * * * * * *
    * * * * * * * * *
    * * * * * * *
    * * * * *
    * * *
    *

  • sushant

    plz help mi to write ths pattern program
    *
    ***
    *****
    ********
    *****
    ***
    *

  • rajani jha

    reallly nyc programming

  • rajani jha

    i want the hint for the program

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

  • tejas chachad

    hey i want to print
    ******
    * *
    * *
    * *
    ******

  • tejas chachad

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

  • santhosh

    @rajani jha

    #include
    main()
    {
    int i,j;
    for(j=4;j>=0;j–)
    {
    for(i=j*2;i>0;i=i-1)
    {
    printf(“*”);
    }
    printf(“\n”);
    }
    }

  • cliff

    please help me with code that displays characters in form of asteriks eg L
    **
    **
    **
    **
    ***********

  • jyothi

    * *
    * *
    *
    * *
    * *

  • shahnawaz

    plz help me to write this program

    54321
    5432
    543
    54
    5

  • manali

    plz tell me how to print the following pattern?
    * * * * * *
    * * * * *
    * * * *
    * * *
    * *
    *

  • rishav

    #include
    int main()
    {
    int m,i,j;
    printf(“enter the size of program \n”);
    scanf(“%d”,&m);
    for(i=0;i<m;i++)
    {
    for(j=0;j<m;j++)
    {
    if(i<=j)
    printf("*");
    }
    printf("\n");
    }
    return 0;}

  • rishav

    #include
    int main()
    {
    int m,n,i,j;
    m=6;
    n=11;
    for(i=0;i<6;i++)
    {
    for(j=0;j<11;j++)
    {
    if(i<5&&j<2)
    printf("*");
    else if(i==5)
    printf("*");
    else
    continue;
    }
    printf("\n");
    }
    return 0;
    }

    here is code for printing………..
    **
    **
    **
    **
    **
    ***********

  • rishav

    here is your code ,,,,,,,,, shahnawaz

    #include
    int main()
    {
    int m,i,j,k,p;
    printf(“enter the size of the program”);
    scanf(“%d”,&m);
    k=p=m;
    for(i=0;i<m;i++)
    {
    for(j=0;j<m;j++)
    {
    if(i<=j)
    {
    printf("%d",p);
    p–;
    }}
    p=k;
    printf("\n");
    }
    return 0;
    }

  • preethi

    tell me the code for

    a
    a a
    a a a
    a a a a
    a a a a a

  • supantha kumar pal

    @preethi
    here is ur programme

    #include
    void main()
    {
    int i,j;
    for(i=1;i<=5;i++)
    {
    printf("\n");
    for(j=1;j<=i;j++)
    printf("a ");
    }
    }

    and ur out put is…

    a
    a a
    a a a
    a a a a
    a a a a a

  • supantha kumar pal

    plz tell me of this code..

    1
    12
    123
    1234
    12345

  • vidhya

    can u tell me how to put tis pattern

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

  • rishav

    dis is ur code ,,,,,,,,,,,,VIDHYA

    #include
    int main()
    {
    int k,m,n,i,j,l;
    printf(“enter the size of the prog:”);
    scanf(“%d”,&n);
    m=n;
    for(j=1;j<=n;j++)
    {
    if((j<=n/2+1))
    {
    k=j*2-1;
    for(l=1;ln/2+1)
    {
    k=m-2;
    m=k;
    for(i=1;i<=k;i++)
    {
    printf("*");
    }
    printf("\n");
    }
    else
    {
    continue;
    }}
    return 0;
    }

  • rishav

    sry dere was problm is pasting d code,,,,,,,,dis is ryt code VIDHYA

    #include
    int main()
    {
    int k,m,n,i,j,l;
    printf(“enter the size of the prog:”);
    scanf(“%d”,&n);
    m=n;
    for(j=1;j<=n;j++)
    {
    if((j<=n/2+1))
    {
    k=j*2-1;
    for(l=1;ln/2+1)
    {
    k=m-2;
    m=k;
    for(i=1;i<=k;i++)
    {
    printf("*");
    }
    printf("\n");
    }
    else
    {
    continue;
    }}
    return 0;
    }

  • http://- Nikhil

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

  • Aman

    code for the patern:
    1
    2 6
    3 7 10
    4 8 11 13
    5 9 12 14 15
    here it goes:

    #include
    #include
    void main()
    {
    int i,j,a,n;
    clrscr();
    printf(“\n Enter the no. of lines to be printed.”);
    scanf(“%d”,&n);
    for(i=1;i<=n;i++)
    {
    printf("\n");
    a=i;
    for(j=1;j<=i;j++)
    {

    printf("%d ",a);
    a=a+n-j;

    }
    }

    getch();
    }

    Enjoy budies…;)

  • Mahendra

    Hey Sushant Ur Code is Here now ,…..Mahendra(Mayur)
    #include
    #include
    void main()
    {
    int i,j,n;
    clrscr();
    printf(“\n Enter the number :”);
    scanf(“%d”,&n);
    for(i=n;i>0;i–)
    {
    for(j=i;j<=n;j++)
    {
    printf("*");
    }
    printf("\n");
    }
    for(i=0;i<=n;i++)
    {
    for(j=i;j<n-1;j++)
    {
    printf("*");
    }
    printf("\n");
    }
    getch();
    }
    /*
    Enter the number :5
    *
    **
    ***
    ****
    *****
    ****
    ***
    **
    *
    */

  • saurabh chopra

    sir,How can we print stars in a circle?

  • supantha kumar pal

    plz tell the code….

    1
    2 3
    4 5 6
    7 8 9 10

  • sai

    to print as
    * * *
    * *
    *

  • http://facebook.com/atubh ATUBH BHAWARE

    plz someone tell me program for following output
    *
    * *
    * * *

    * * * * *

  • http://facebook.com/atubh ATUBH BHAWARE

    plz someone tell me program for following output
    * *
    * * * *
    * * * * * *
    * * * * * * * *
    * * * * * * * * * *
    * * * * * * * *
    * * * * * *
    * * * *
    * *

  • Ashu

    thanks . this is very helpful

  • http://www.interviewmantra.net/2009/10/10-star-pattern-programs.html#ques1 nitesh

    this is the best for bigners

  • jyotish

    plz someone tell me program for following output

    abcdedcba
    abcd dcba
    abc cba
    ab ab
    a a

  • jadoo

    #include
    #include
    #include
    #include
    #include
    #include
    #include

    using namespace std;

    int main()
    {
    for(int i=1;i<=5;i++)
    {
    for(int j=1;j<=i;j++)
    {
    cout<<"**";
    }
    cout<=1;i–)
    {
    for(int j=i;j>=1;j–)
    {
    cout<<"**";
    }
    cout<<endl;
    }
    return 0;
    }

  • accesscode

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

    Try this>>>>>

  • ajit sujit

    ye s this is perfect .but it will not run properly so modify it now.

  • amit sisodiaa

    this is so goood for begainer

  • http://www.gmail.com saurabh

    heloo friends your mind is very sharp becouse your programing is very dilicious

  • Rajitha

    1 1 1 1 1 1 1
    1 2 2 1
    1 3 3 1
    1 4 1
    1 3 3 1
    1 2 2 1
    1 1 1 1 1 1 1
    can any one do this?????????

  • jasleen

    can u please give me a coding of this pattern?
    *

  • jasleen

    can u please give me a coding of this pattern?
    *
    * *
    * *
    * * * * * * *

  • jasleen

    above is not exact pattern may b some problem in printing
    the pattern is :
    there is three spaces and then star in first line
    in second line two spaces then star then one space and again star
    in third line one space one star then three spaces one star
    in fourth line seven stars.

  • sushant choudhary

    code to print the following pattern:
    1
    1 3 1
    1 3 5 3 1
    1 3 5 7 5 3 1
    1 3 5 3 1
    1 3 1
    1

  • http://jevie.webs.com jevie mar galaura

    i have problem in my codes please sent me and email on asterisk patterns please!!!!!!!!

  • http://no. Vjy

    1
    1 4 1
    1 3 5 3 1
    1 3 5 7 5 3 1
    1 3 5 3 1
    1 4 1
    1

  • R.Mahalakshmi

    i need simple c program to print like a following pattern
    *
    * * *
    * * * * *
    * * *
    *

  • loveneesh

    how can i print the pattern

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

  • loveneesh

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

  • loveneesh

    mirror image of this
    *****
    ****
    ***
    **
    *

  • http://thewebanalyzers.com ankur loved

    this is not the optimized code this will use more memory

  • KALE SOMNAH

    PLS tell me code to wether the given strig s palioderm or not

  • S.V.Ramana

    This is code for Loveneesh’s output like
    *****
    ****
    ***
    **
    *

    #include
    #include
    void main()
    {
    int i,j,n;
    clrscr();
    printf(“Enter n value”);
    scanf(“%d”,&n);
    for(i=n;i>0;i–)
    {
    for(j=1;j<=i;j++)
    {
    printf("*");
    }
    printf("\n");
    }
    getch();
    }

  • Mallickanurag25

    #include
    void main()
    {
    int r=0,x,y,z;
    printf(“Enter the number to be checked “);
    scanf(“%d”,&x);
    y=x;
    while (x!=0)
    {
    r=(r*10)+(x%10);
    x=x/10;
    z=r;
    }
    if (y==z)
    {
    printf(“the given number is a pallindrome.”);
    }
    else
    {
    printf(“the given number is not a pallindrome.”);
    }
    }

  • Mallickanurag25

    for (int i=1; i<6;i++)
    {
    for(int j=5; ;)

  • rashmi

    plz tell me coding ******  and send me code on my email-id
                                         *         * my id smile_rashmicharjan@rediff.com
                                         *         *
                                         ******

  • Manish Prajapati1991

    please give me help for printing below pattern.
    if inputn is 5 then
    1      2    3    4    516  17  18  19  6
    15  24  25  20 7 
    14  23  22  21  8   
    13  12  11  10  9

  • Samadhan Nirali

    void main()
    {
    int i,j,k=1;
    for(i=1;i<=5;i++)
    {
    for(j=1;j<=k;j++)
    {
    printf("%d",j);
    }
    k++;
    printf("n");
    }
    getch();
    }

  • Snehalvjadhav

    tell me the code of following pattern.

                  *
                *  *
             *   *  *
                *   *
                   *

  • Dharmu

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

  • Dharmu

    #include
     
    void main()
    {
    int n, c, k;
     
    printf(“Enter number of rowsn”);
    scanf(“%d”,&n);
     
    for ( c = 1 ; c <= n ; c++ )
    {
    for( k = c ; k <= n ; k++ )
    printf("*");
     
    printf("n");
    }
     

    }

  • Dharmu1994

    #include
     
    main()
    {
    int n, c, k,p=1;
     
    printf(“Enter number of rowsn”);
    scanf(“%d”,&n);
     
    for ( c = 1 ; c <= n ; c++ )
    {
    for( k = 1 ; k <= c ; k++ )
    { printf("%d",p); p=p+1;
      }
    printf("n");
    }
     
    return 0;
    }

  • Himkarjha2042

    #include
    #include
    void main()
    {
    int i,j,;
    clrscr();

    for(i=6;i>0;i–)
    {
    for(j=1;j<=i;j++)
    {
    printf("*");
    }
    printf("n");
    }
    getch();
    }

  • siva

    so diff to get!!!

  • Rohitarg91

    great

  • Sudhir

    please can you provide the code for following pattern in C++ 

    H           H       EEEEE    L                 L                      OOOOH           H       E               L                 L                   O           OH           H       E               L                 L                   O            OHHHHH        EEEEE     L                 L                   O           OH           H       E                L                 L                   O           OH           H       E                L                 L                   O           OH           H       EEEEE      LLLLL       LLLLL        OOOOO

    its a little distorted but you must have understood what i am trying to printcan mail me at SUDHIR14789@gmail.com

  • Mansi

    #include
    main()
    {
    int i,j,k,n;
    printf(how many lines you want to enter:”);
    scanf(“%d”,&n);
    for(i=1;i=i;j–)
    {
    printf(” “);
    }
    for(k=1;k<=i-1;k++)
    {
    printf("*");
    printf("  ");
    }
    printf("n");
    }
    for(i=1;i<=n;i++)
    {
    for(j=1;j=i;k–)
    {
    printf(“*”);
    printf(” “);
    }
    printf(“n”);
    }
    }

  • Bschinku

    thanx dear

  • Saptapratim Bose

    for(j=1;j<=5;j++)
    {
         for(i=5;i,=j;i–)
              printf('i");
                 printf("n"0;
    }
    Reply saptapratim Bose

  • Saptapratim Bose

    for(j=1;j<=5;j++)
    {
         for(i=5;i,=j;i–)
              printf('i");
                 printf("n");
    }
    Reply saptapratim Bose

  • anonymous

    *       *
      *   *
         *
      *   *
    *       *
    no. of rows to be entered by user ?

  • hello

    give me the code of following pattern
          1
         1 1
       1 2 1
      1 3 3 1
     1 4 6 4 1 

  • abc

    give me the code of following pattern
          *
        ***
      *****
    *******

  • Abiha_kazmi32

    tell me the code of following
                    *
                  ***
                 ****
                *****
                 ****
                  ***
                    *

  • Hari

    how can i print  ’**’s  four sides of screen????
    example  like  this…………
    ************************************************************************
    *                                                                                                                                                                     *  
    *                                                                                                                                                                     * 
    *                                                                                                                                                                     *
    *                                                                                                                                                                     *
    *                                                                                                                                                                      *
    *                                                                                                                                                                      *
    *                                                                                                                                                                      *
    *                                                                                                                                                                      *                       
    *                                                                                                                                                                      *
    *                                                                                                                                                                      *
    *************************************************************************

    can any 1 help me??????please…….

  • Bujji

    Aim        : 
    to find whether given string is palindrome or not

       Input      
    : accept a string

       Output    
    : palindrome or not

    Program :

    #include

    int main(void)

    {

                 char st[20],rev[20];

                 int l=0,i;

                 clrscr();

                 printf(“enter a string”);

                 scanf(“%s”,&st);

                 for(;st[l]!=”;l++);                   /*to
    find length of a string*/

                 for(i=0;i<l;i++)

                                 rev[i]=st[l-i-1];        /*to reverse the string*/

                 rev[l]='';

                 for(i=0;i<l;i++)

                 {

                  
    if(st[i]!=rev[i])       /*to compare
    original string & reversed string*/

                                 break;                                       /*to
    break the loop*/

                 }

                 if(i==l)                                     /*to
    compare lengths of 2 srings*/

                   
    printf("palindrome");

                 else

                   
    printf("not a palindrome");

    }

    INPUT  OUTPUT:

    enter a string

    madam

    palindrome

     

    enter a string

    bunny

    not a palindrome

  • subbanaidu

    #include
    void main()
    {int i,j,temp=0;
    clrscr();
    for(i=0;i<n;i++)
    for(j=0;j<n;j++)
    {
    printf("%d",temp);
    temp++;
    }
    }

  • Sarvesh445

    void main()
    {
      int a,b;
    a=b=7/2+1;//n=no of rows..n+(n-1)=7(for 4 rows)
    for(int i=1;i<=4;i++)
    {
    for(int j=1;j=a&&j<=b)
    print("*");
    }
    a–;
    b++;
    }
    }

  • Sarvesh445

    void main()
    {
    for(int i=1;i<=n;i++)//n is any integer value
    for(int j=1;j<=n;j++)
    if(i==1||i==n||j==1||j==n)
    print("*");
    }

  • deepak singla

    give me the code of following pattern
    1
    01
    101
    0101
    10101

  • Suhani6yadav

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

  • Rajsharmaj2ee

    give me the code of following  character A  B  C  D  E  F  G  F  E  D  C  B  A
    A  B  C  D  E  F       F  E  D  C  B  A
    A  B  C  D  E                E  D  C  B  A 
    A  B  C  D                          D C   B  A
    A  B  C                                   C   B  A
    A  B                                              B  A
    A                                                       A

    pls reply sir code
    regards
    Raj vishwakarma

  • Grissar

    Would someone be able to help with creating a hollow triangle in Java looking like this:
    —-*——-*-*—–*—*—*—–*-*********
    Where the – equals a space. A user enters a number and the triangle height = the number. 
    I have this code so far but I can’t get any further. Not even sure if I’m on the right track anymore: http://pastebin.com/rUrKaVW5 Thanks in advance.

  • Grissar

    The drawing didn’t come out ok. So, here’s a photo.

  • Ajay

    please giv me the code for following pattern!
    1
    11
    21
    1211
    111221
    312211
    13112221

  • Bhomit Davara

    Give me the code of following pattern :
    when we input : 3

              6
           5    4
        3    2     1

  • Dineshrockzzz8

    class pattern21st
    {
        public static void main ()
        {
            for(int i = 1 ; i <=5 ; i++)
            {
                for(int j = 1 ; j =1 ; i–)
            {
                for(int j = 1 ; j <= i ; j++)
                {
                    System.out.print( "*" + " " );
                }
                System.out.println();
            }
        }
    }   

  • Dineshrockzzz8

    class pattern1st
    {
        public static void main ()
        {
            for(int i = 1 ; i <=5 ; i++)
            {
                for(int j = 1 ; j <= i ; j++)
                {
                    System.out.print( "*" + " " );
                }
                System.out.println();
            }
        }
    }   

  • Bindu Kamboj

    #include
    #include
    void main()
    int i,j,k,l,a=1,d=2,x;
    char p=’A';
    for(i=1;i<=6-1;i++)
    {
      for(j=1;j<=7-i;j++)
      {
        printf("%c",p);
        p++;
      }
    x=a+(i-1)*d;
     for(k=1;k<=x;k++)
      {
        printf(" ");
      }
     for(l=1;l<=7-i;l++)
     {
       printf("%c",p-1);
       p–;
     }
    printf("n");
    }
    getch();
    }

  • Bindu kamboj

    #include
    void main()
    {
    int i,j,a=1;
    for(i=1;i<=4;i++)
     {
      for(j=1;j<i+1;j++)
       {
       printf("%d",a);
       a++;
       }
    printf("n");
     }
    getch();
    }

  • Bindu kamboj

    #include
    void main()
    {
    int i,j,a=1;
    for(i=1;i<=4;i++)
     {
      for(j=1;j<i+1;j++)   {
       printf(“%d”,a);
       a++;
       }
    printf(“n”);
     }
    getch();
    }

  • Bindu kamboj

    #include
    void main()
    {
    int i,j,a=6;
    for(i=1;i<=3;i++)
     {
      for(j=1;j<i+1;j++)   {
       printf(“%d”,a );
       a–;
       }
    printf(“n”);
     }
    getch();
    }

  • Angad tomar

    for(i=n;i>0;i–)
    {
      printf(”  ”);
      for(j=1;j<=i;j++)
      {
        printf("*");

      }
      printf("n");

    }

  • Jayesh5101992sapkale

    Hi Hari ,,,,,Check this code in c language……….if any Error Plz Fix it….

    Code:

    #include #include int first_term=1;void main(){int a[100][100];int c=1,i=0,j=0,k=0,m=0,n=0,x=0;clrscr();printf(“Enter the number of rows and columns for the square matrix n”);scanf(“%d”,&n);x=n;while(n>=1){for(k=0;k<n;k++)if(first_term){a[m][k+m]=c++;}else{a[m][k+m]=0;}for(k=1;k=0;k–)if(first_term){a[n-1+m][k+m]=c++;}else{a[n-1+m][k+m]=0;}for(k=n-2;k>0;k–)if(first_term){a[k+m][m]=c++;}else{a[k+m][m]=0;}if(x==n){first_term=0;}n-=2;m++;}for(i=0;i<x;i++){for(j=0;j<x;j++){  if(a[i][j]==0){printf("     ");}  else{printf("    *") ;}}printf("n");}getch();}

  • Jayesh5101992sapkale

    sorry this is not suitable.,,,,I will provide in to parts

  • Jayesh5101992sapkale

    #include
    #include

    int first_term=1;

    void main()
    {
    int a[100][100];int c=1,i=0,j=0,k=0,m=0,n=0,x=0;clrscr();
    printf(“Enter the number of rows and columns for the square matrix n”);scanf(“%d”,&n);x=n;

  • Jayesh5101992sapkale

    can U give your Email id I Will Send u program solution  b-coz content not Fit on it,,,,,,,,,,,,,,,,If You have Already Solved this problem then Very Good Try………..

  • Jayesh5101992sapkale

    int first_term=1;

    void main()
    {

    int a[100][100];
    int c=1,i=0,j=0,k=0,m=0,n=0,x=0;

    clrscr();
    printf(“Enter Rows and columns for the square matrix n”);
    scanf(“%d”,&n);
    x=n;
    while(n>=1)
    {
    for(k=0;k<n;k++)
    if(first_term){a[m][k+m]=c++;}
    else{a[m][k+m]=0;}

    for(k=1;k=0;k–)
    if(first_term){a[n-1+m][k+m]=c++;}
    else{a[n-1+m][k+m]=0;}

    for(k=n-2;k>0;k–)
    if(first_term){a[k+m][m]=c++;}
    else{a[k+m][m]=0;}

    if(x==n){
    first_term=0;}
    n-=2;
    m++;

    }

    for(i=0;i<x;i++)
    {
    for(j=0;j<x;j++)
    {
      if(a[i][j]==0){printf("     ");}
      else{printf("    *") ;}
    }

    printf("n");
    }
    getch();
    }

  • Jayesh5101992sapkale

    Remove all last line from it

  • Nkkoolguy

    *

    * *

     
    *

    * * * *

        *

        * *

          *

    * * * * * * * *

            *

            * *

              *

            * * * *

                *

                * *

                  *Plz tell the code to get this pattern

  • Ridzi0207

    plz tell me the code for
    *******
    *******
         ***
           *
         ***
    *******

Previous post:

Next post: