× Home
Next Lec → ← Previous Lec

Printing star pattern exercise

/* Take input from the user and ask user to choose 0 for triangular star pattern and 1 for reverse triangular pattern
Then print the pattern accordingly
*
**
***
****
***** → triangular star pattern


*****
****
***
**
* → Reversed triangular star pattern */

                        

                                #include <stdio.h>
                                void starPattern(int rows)
                                {
                                    for (int i = 0; i < rows; i++)
                                    {
                                        for (int j = 0; j <= i; j++)
                                        {
                                            printf("*");
                                        }
                                
                                        printf("\n");
                                    }
                                }
                                void revStarPattern(int rows)
                                {
                                    for (int i = 0; i < rows; i++)
                                    {
                                        for (int j = 0; j <= rows - i - 1; j++)
                                        {
                                            printf("*");
                                        }
                                
                                        printf("\n");
                                    }
                                }
                                int main()
                                {
                                    int rows, type;
                                    printf("type 0 - star pattern \n type 1 - reverse star pattern :");
                                    scanf("%d", &type);
                                    printf("\nHow many rows do you want ? :");
                                    scanf("%d\n", &rows);
                                    if (type == 0)
                                    {
                                        starPattern(rows);
                                    }
                                    else if (type == 1)
                                    {
                                        revStarPattern(rows);
                                    }
                                    else
                                    {
                                        printf("You have entered an invalid choice");
                                    }
                                
                                    return 0;
                                } // Writing the reverse star code like this also works
                                // void revStarPattern(int rows)
                                //{
                                // for (int i = rows-1; i >= 0; i--)
                                // {
                                // for (int j = 0; j <= i; j++)
                                // {
                                // printf("*");
                                // }
                                //
                                // printf("\n");
                                // }
                                //}