Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Star Program in C

Last Updated on June 6, 2023 by Mayank Dham

A pattern is something that repeats a specific number of times. In actuality, its pattern could be beautiful, like a rangoli, for instance. a C program for improving algorithms and looping concepts. It is easy to demonstrate loops using nesting. The for loop is simpler to write into programs than the while loop, even though both may be used to build loops. While columns are presented using the inner loop, rows are displayed using the outer loop (assume Matrix). The outer loop controls the number of rows, while the inner loop displays the values in those columns as a result.
Using either a "*" Star or another character, we will make the patterns. We’ll design various patterns or geometric forms like triangles, squares, etc. A pattern that repeats itself a certain number of times is known as a pattern. With a solid understanding of the loops, namely the for and while loops, In a pattern program written in C, nested loops are typically used to manage both the number of rows and the data in each row that comprises the text to be displayed.We will be discussing star program in C.

Algorithm to Print Star Pattern in C

  • Print the star pattern from the end of each Row
  • Complete the last column of each Row
  • Start from the Second Last Column of the second row
  • Repeat till the number of rows is specified by the User.

Top 5-Star Pattern Program in C:

The following are some most asked star pattern program in C language.

Square Star Pattern:

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

The code to create the square star pattern is given below:

#include <stdio.h>  
 int main()  
{  
    int n;  
    printf("Enter the number of rows");  
    scanf("%d",&n);  
    for(int i=0;i<n;i++)  
    {  
        for(int j=0;j<n;j++)  
        {  
            printf("*");  
        }  
        printf("\n");  
    }  
      
    return 0;  
}

Inverted Half Pyramid

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

The code for the right triangle star pattern is given below:

#include <stdio.h>
int main() {
   int i, j, rows;
   rows = 5;
   for (i = rows; i >= 1; --i) {
      for (j = 1; j <= i; ++j) {
         printf("* ");
      }
      printf("\n");
   }
   return 0;
}

Mirrored Right Triangle Star Pattern

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

The code for the mirrored right triangle star pattern is given below:

#include <stdio.h>  
  
int main()  
{  
    int n,m=1;  
    printf("Enter the number of rows");  
    scanf("%d",&n);  
    for(int i=n;i>=1;i--)  
    {  
        for(int j=1;j<=i-1;j++)  
        {  
          printf(" ");  
        }  
        for(int k=1;k<=m;k++)  
        {  
            printf("*");  
        }  
        printf("\n");  
        m++;  
    }  
    return 0;  
}

Pyramid Star Pattern

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

The code for the pyramid star pattern is given below:

#include <stdio.h>  
int main()  
{  
    int n,m;  
    printf("Enter the number of rows");  
    scanf("%d",&n);  
    m=n;  
   for(int i=1;i<=n;i++)  
   {  
       for(int j=1;j<=m-1;j++)  
       {  
           printf(" ");  
       }  
       for(int k=1;k<=2*i-1;k++)  
       {  
         printf("*");  
       }  
       m--;  
     
      printf("\n");  
    }  
    return 0;  
}

Hollow Pyramid Star Pattern

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

The code for the inverted pyramid is given below:

#include <stdio.h>  
  
int main()  
{  
    int n,m=1;  
    n = 5; 
  
   for(int i=n;i>=1;i--)  
   {  
       for(int j=1;j<m;j++)  
       {  
           printf(" ");  
       }  
       for(int k=1;k<=2*i-1;k++)  
       {  
          if(k==1 || k==2*i-1 || i==n)  
           printf("*");  
           else  
           printf(" ");  
       }  
       m++;  
     
      printf("\n");  
    }  
    return 0;  
}

Conclusion:
In conclusion, the star program in C is a powerful tool for creating various geometric patterns using nested loops. By leveraging the for and while loops, we can control the number of rows and the data in each row to generate visually appealing patterns. This article presented the algorithm for printing star patterns and showcased the top 5 star pattern programs in C, including the square star pattern, inverted half pyramid, mirrored right triangle, pyramid, and hollow pyramid star patterns. These examples demonstrate the versatility and elegance of the star program in C, providing programmers with endless possibilities for creating captivating patterns. Whether it’s a simple square or a complex geometric shape, the star program in C opens up a world of creativity and algorithmic thinking.

Frequently Asked Questions (FAQs)

Q1.What is the purpose of the star program in C mentioned in the article?
The star program in C is designed to create various geometric patterns using nested loops, providing a way to demonstrate algorithmic concepts and improve understanding of looping in C programming.

Q2.What are the advantages of using the for loop over the while loop in the star program in C?
The for loop is often preferred in the star program in C because it is simpler to write and allows for concise control over the number of iterations. It is especially useful for managing rows and columns in pattern printing.

Q3.How are nested loops used in the star program in C to generate patterns?
Nested loops are utilized to control the number of rows and the data in each row in pattern printing. The outer loop determines the number of rows, while the inner loop handles the values in the columns, resulting in the desired pattern formation.

Q4.Can you explain the algorithm mentioned in the article for printing star patterns in C?
The algorithm presented in the article suggests starting from the end of each row and completing the last column of each row. Then, it suggests starting from the second last column of the second row and repeating the process until the specified number of rows is reached.

Q5.Are there any other popular star pattern programs in C apart from the ones mentioned in the article?
Yes, there are numerous other star pattern programs in C that programmers often explore. Some examples include diamond star pattern, Pascal’s triangle, number pattern, staircase pattern, and more. These patterns provide additional challenges and opportunities for creativity in programming.

Leave a Reply

Your email address will not be published. Required fields are marked *