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

We will learn how to design patterns using the C programming language in this topic. 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.

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.

Algorithm Used to Print Star Pattern

  • 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:
So far, we’ve seen some star program in C for printing different Star patterns. How to print the inverted hollow star pyramid pattern and the Star pyramid pattern with the specified number of rows. Includes formulas to help you learn and comprehend things step by step.

Other C Programs

C Program for Binary Search
C Program to Add Two Numbers
C Program to Calculate Percentage of 5 Subjects
C Program to Convert Binary Number to Decimal Number
C Program to Convert Celsius to Fahrenheit
C Program to Convert Infix to Postfix
C Program to Find Area of Circle
C Program to Find Roots of Quadratic Equation
C program to Reverse a Linked List
C program to reverse a number
Ascending Order Program in C
Menu Driven Program For All Operations On Doubly Linked List in C
C Program for Armstrong Number
C Program For Merge Sort For Linked Lists
C program for performing Bubble sort on Linked List
Hello World Program in C
Perfect Number Program in C
Leap Year Program in C
Odd Even Program in C
Selection Sort Program in C
Linear Search Program in C
While Loop Program in C
C Program to Swap Two Numbers
Calculator Program in C Language
Simple Interest Program in C
Compound Interest Program in C
Priority Scheduling Program in C
Doubly Linked List Program in C
FCFS Scheduling Program in C
Insertion Sort Program in C
Singly Linked List Program in C

Leave a Reply

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