10 Challenging star pattern programs in C


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 <stdio.h>
    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<stdio.h>
    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<stdio.h>
    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<stdio.h>
    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<stdio.h>
    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 <stdio.h>
    // 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 <stdio.h>
    
    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 <stdio.h>
    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 <stdio.h>
    /*
     * 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 <stdio.h>
    
    /*
     * 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


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



All comments of post - "10 Challenging star pattern programs in C":

:Haha! I'am the first! Yeh~

Thank you!

Add a Comment / Trackback url

  1. #9  Devender MishraNo Gravatar

    @ 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";
    }

    10/07/27 19:34
  2. #8  harpreetNo Gravatar

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

    10/05/03 09:44
  3. #7  NipunNo Gravatar

    Hay many many thanks for give a nice shape of star

    10/02/16 18:46
  4. #6  abhang savitaNo Gravatar

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

    10/02/02 15:45
  5. #5  adminNo Gravatar

    Thanks Shyamala. Glad that you liked our examples.

    10/01/13 11:05
  6. #4  Shyamala VydyamNo Gravatar

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

    10/01/07 16:45
  7. #3  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……….

    09/11/17 10:06
  8. #2  sridhar

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

    09/10/29 23:23
  9. #1  Anonymous

    thats good job keep it up

    09/10/29 23:22

Show all Show 5 so far Close all

Comment begin from here or jump up!