Star patterns program in C

Star patterns are a series of '*' or any other character that are used to create some patterns or any geometrical shape such as - square, triangle(Pyramid), rhombus, heart etc. These patterns are often prescribed by many programming books and are best for practicing loops in programming and to enhance logical thinking capability. Before printing any star pattern you must have knowledge of loops and basic of pattern printing (Logic to print star patterns and shape).

Below is a list of easy and hard star patterns with explanation. Practice as much as you can to increase your logical thinking.
Stars pattern in C

List of various star pattern series and solution in C:

*****
*****
*****
*****
*****
1. Square
*****
*   *
*   *
*   *
*****
2. Hollow Square


    *****
   *****
  *****
 *****
*****
3. Rhombus
    *****
   *   *
  *   *
 *   *
*****
4. Hollow Rhombus


*****
 *****
  *****
   *****
    *****
5. Mirrored Rhombus
*****
 *   *
  *   *
   *   *
    *****
6. Hollow mirrored Rhombus


*
**
***
****
*****
7. Right triangle
*
**
* *
*  *
*****
8. Hollow right triangle


    *
   **
  ***
 ****
*****
9. Mirrored
right triangle
    *
   **
  * *
 *  *
*****
10. Hollow mirrored
right triangle


*****
****
***
**
*
11. Inverted
right triangle
*****
*  *
* *
**
*
12. Hollow inverted
right triangle


*****
 ****
  ***
   **
    *
13. Inverted mirrored
right triangle
*****
 *  *
  * *
   **
    *
14. Hollow inverted
mirrored right triangle


    *
   ***
  *****
 *******
*********
15. Pyramid
(Equilateral triangle)
    *
   * *
  *   *
 *     *
*********
16. Hollow Pyramid


*********
 *******
  *****
   ***
    *
17. Inverted Pyramid
*********
 *     *
  *   *
   * *
    *
18. Hollow inverted pyramid


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

19. Half diamond
    *
   **
  ***
 ****
*****
 ****
  ***
   **
    *
20. Mirrored
half diamond


    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
21. Diamond
**********
****  ****
***    ***
**      **
*        *
*        *
**      **
***    ***
****  ****
**********
22. Hollow diamond


*****
  ****
    ***
      **
        *
      **
    ***
  ****
*****
23. Right Arrow
    *****
   ****
  ***
 **
*
 **
  ***
   ****
    *****
24. Left arrow


    +
    +
    +
    +
+++++++++
    +
    +
    +
    +
25. Plus Star pattern
*       *
 *     *
  *   *
   * *
    *
   * *
  *   *
 *     *
*       *
26. X Star pattern


  *****     *****
 *******   *******
********* *********
*******************
 *****************
  ***************
   *************
    ***********
     *********
      *******
       *****
        ***
         *
27. Heart Star pattern 1
  *****     *****
 *******   *******
********* *********
*****Codeforwin****
 *****************
  ***************
   *************
    ***********
     *********
      *******
       *****
        ***
         *
         *
28. Heart Star pattern 2
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

6 comments:

  1. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from C Programming Hub

    ReplyDelete
    Replies
    1. Thanks, giving my best. Keep visiting.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. hello Pankaj..
    I want to write a menu oriented dynamic program, which will be both flow forward and flow backward type.
    the structure of the program will be like
    Main menu->Menu->Sub-menu and so on..
    The main thing in this program is that at the end of every sub-menu, i want to put two options:
    " 1. to go back to previous menu, press ____ .
    2. to go back to main menu, press _____ ."
    i know that to put menu in the program, i will have to use switch-case statements.
    but what will be the code to put those two options mentioned above in the program.
    you may advice to use fflush(stdin) after each sub menu, but it is useful only to deal with first option. I want to deal with both the optinos simultaneously.
    what should be the proper coding??
    I am writing my program in C Language.

    ReplyDelete
    Replies
    1. Hello ankit,

      Here I am giving the basic logic to create menu oriented program in C that fits your need. I won't be writing full program here. You can use the below approach.

      #include <stdio.h>
      #include <conio.h> //Used for clrscr()

      int main()
      {
          int choice;

          //Runs forever until user selects 3
          while(1)
          {
              choice = printHomeMenu();
              switch(choice)
              {
                  case 1: function1(); break;
                  case 2: function2(); break;
                  case 3: return 0;
                  default: printf("Invalid choice try again");
                           system("pause");
              }
          }
      }

      int printHomeMenu()
      {
          int choice;
          
          //You may want to clear the screen
          //before printing menu on home screen
          clrscr();

          printf("YOUR WELCOME MESSAGE\n");
          printf("--------------------\n");
          printf("1. Function 1\n");
          printf("2. Function 2\n");
          printf("3. Exit\n");
          printf("--------------------\n");
          printf("Enter your choice(1-3):");
          scanf("%d", &choice);
          
          return choice;
      }

      int printSubMenu()
      {
          int choice;
          printf("--------------------------\n");
          printf("1. Some function"\n);
          printf("0. Main menu\n");
          printf("---------------------------\n");
          printf("Enter your choice: ");
          scanf("%d", &choice);

          return choice;
      }

      function1()
      {
          int choice;

          while(1)
          {
              choice = printSubMenu();
              //Do some pther task based on choice
              switch(choice)
              {
                  case 1: //do some task
                          break;
                  case 0: return 0; //return to main menu
                  default: printf("Invalid choice enter again");
                           system("pause");
              }
          }
      }

      function2()
      {
          //Logic similar as function1
      }

      Delete
    2. thank you so much bro...
      i will try with this sample code, and will contact u with the results!!!!
      Chat Conversation End

      Delete