How to Print Star Pattern Programs Using C Language?

Posted by mira smith on May 28th, 2019

C is a high-level and structure oriented computer programming language. It was developed by Dennis Ritchie at Bell Laboratories in 1972. Since it is a high-level language, it requires a lot of practice for coding. One of the major challenging tasks that students face while coding is to print a star pattern program as it involves so many steps. But, with this article, working on the same will become much easier for you. Just follow the 7 simple steps mentioned below:
 
The following section will let you know how to make a star pattern using three ‘for’ loops:
 
How to Print a Star Pattern Program?
 
#include <stdio.h>
int main()  
{
   int rows, star, spaces;
   int number_of_stars = 6;
   int number_of_rows = number_of_stars;
for (rows=1; rows <= number_of_rows; rows++)  
{
   for (spaces=1; spaces <= number_of_stars; spaces++)  
{
     printf(" ");
}
    for (star=1; star <= rows; star++)  
{
    printf("*");
    printf(" ");
}
   printf("\n");
   number_of_stars = number_of_stars - 1;
}
   return 0;
}
 
OK!!! So, this is the basic code, i.e., how to print a star pattern using C language. Now, let’s start with the step-by-step explanation of the above-mentioned code.
 
Step-by-Step Explanation: How to Print a Star Pattern Program
 
Step 1: The first step is #include <stdio.h>. It is basically a Pre-processor command which tells the C compiler to include “stdio.h” file before starting with the actual compilation.  
 
Step 2: The next step is the int main(); It is the main function from where the execution of the program begins.
 
Step 3: Next, start declaring the variable for example  

The number_of_stars denotes the number of stars at the base of the pyramid. Like in above-mentioned code, we have mentioned the no. of stars = 6, means at the last row there will be six stars.

The number_of_rows denotes the height of the pyramid
 
Step 4: Next, we have to define the number_of_rows = number_of_stars; It simply means that the number of rows and stars will be the same.
 
Step 5: We will then set up a “for loop” that will divide the pyramid into no of rows. The for loop is basically used to execute a sequence of statements multiple times. It also abbreviates the code that is used to manage the loop variable.
 
Step 6: In this step, insert a value in the for loop:

In the first “for loop,” the stars are divided into a number of rows.  

In the second “for loop,” a certain number of spaces before a star is given. Using this loop, you can place a star at a certain point in each row.

Now, the actual placing of the stars is done in the third “for loop”. This loop is used to print the number of stars that are needed for a certain row.

 
Step 7: In the last step, end a row with a newline escape sequence character. All you have to do is just reduce down the number_of_stars by one.
 
This is all about - how to code star pattern in C language. Now the output will look like this:

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

So, these are the simple steps to print a star pattern program using C language. The students who face problems in printing a star pattern can surely be benefited by following the above-mentioned steps. However, if you face some difficulties in writing a different program and unable to complete the task given, then should seek C programming assignment help from the experts.

Like it? Share it!


mira smith

About the Author

mira smith
Joined: January 15th, 2018
Articles Posted: 13

More by this author