Print Star Pattern in C (***)

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses
Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest in 3 Numbers
Find Grade of student
Find the absolute value
Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Print Star Pattern in C. In the previous C program examples, we have developed many pattern programs in C to display patterns of the star, numbers, alphabets, and their combinations. Now in this post, we see many star patterns programs in C. The sample input and output of star patterns are given, observe them and write star program in C. 

While printing star patterns in C, we need to use nested loops. The outer loop represents rows of the star pattern and the inner loop represents columns of the star pattern. The star symbol will be displayed within columns through the inner loop.

The loop can be for loop, while loop, do-while loop. But using for loop to print star pattern in C is easier compared to while loop and do-while loop. Therefore we will use a for loop to print the star pattern in C. To iterate we will use the temporary variable “i” representing a row in the outer loop, and “j” representing a column in the inner loop.

Print Star Pattern in C – 1

Let us start with the very basic star pattern. Write a program for the below star pattern in C. 

Sample input/output are:-

N = 4
****
****
****
****

N = 6
******
******
******
******
******
******

In this star pattern, the input N contains N rows, and every row contains N columns. Example:- when n = 4, then number of rows = 4, and columns = 4. Similarly, when n = 6, then the number of rows = 6, and columns = 6. Therefore the outer loop will go from 1 to N, and the inner loop will also go from 1 to N. 

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses
#include<stdio.h>
int main()
{
   int n;

   printf("Enter the number of rows: ");
   scanf("%d", &n);

   // outer for loop
   for(int r = 1; r <= n; r++)
   {
     // inner for loop
     for(int c = 1; c <= n; c++)
     {
       printf("*"); // star
     }
     printf("\n"); // new line
   }

   return 0;
}

Output:-

Enter the number of rows: 4
****
****
****
****

Half Pyramid of Star Program in C

Let us develop some C programs for the Half pyramid star pattern. In these programs increment and decrement operators can manipulate the results.

Star Pattern Program in C – 2

Sample input/output are:-

N = 5
*
**
***
****
*****

N = 7
*
**
***
****
*****
******
*******

In this star pattern, when n = 5, then there are 5 rows, 1st row contains 1 column, 2nd row contains 2 columns, 3rd row contains 3 columns, 4th row contains 4 columns, and 5th row contains 5 columns. Similarly, when n = 7, then there are 7 rows, 1st row contains 1 column,  2nd row contains 2 columns, and so on.

Based on these, we can conclude for the input N, there are N rows, and every ith row contains i number of columns. The number of columns completely depends upon the row number. Therefore the row (outer loop) will go from 1 to N. But in the ith row, the column (inner loop) will go from 1 to i.

There are many ways to print this star pattern. We can use increment, and decrement operator or their combination in the outer and inner loop.

Program description:- Print half pyramid of star pattern in C using increment operator in both outer and inner loop.

In this pattern, we will use increment operators both in the outer and inner loops. The outer loop starts from 1 and goes till N. Similarly, in the ith row-column will start from 1 and go till i.

#include<stdio.h>
int main()
{
   int n;

   printf("Enter the number of rows: ");
   scanf("%d", &n);

   // outer for loop 
   for(int i = 1; i <= n; i++)
   {
      // inner for loop 
      for(int j = 1; j <= i; j++)
      {
         printf("*");
      }
      printf("\n");
   }

   return 0;
}

The output of the above program:-

Enter the number of rows: 7
*
**
***
****
*****
******
*******

Program description:- Print half pyramid of star pattern in C using decrement operator in the outer loop, and increment operator in the inner loop.

How can we do this? In this star program in C, the outer loop will go from N to 1, and the inner loop will go from i to N, where i is the row number. So, when i = 5, then go from 5 to 5, when i = 4 then go from 4 to 5, when i = 3 then go from 3 to 5.

#include<stdio.h>
int main()
{
   int n;

   printf("Enter the number of rows: ");
   scanf("%d", &n);

   // outer loop with decrement operator
   for(int i = n; i > 0; i--)
   {
      // inner for loop with increment operator
      for(int j = i; j <= n; j++)
      {
         printf("*");
      }
      printf("\n");
   }

   return 0;
}

Program description:- Print half pyramid of star pattern in C using increment operator in the outer loop, and decrement operator in the inner loop.

Opposite to the previous program, this time we have to use the increment operator in the outer loop, and the decrement operator in the inner loop. How to do that? This outer loop will go from 1 to N, but the inner loop will go from i to 1. Now, when i = 1 then go from 1 to 1, when i = 2 then go from 2 to 1, when i = 3 then go from 3 to 1, and so on.

#include<stdio.h>
int main()
{
   int n;

   printf("Enter the number of rows: ");
   scanf("%d", &n);

   // outer loop with decrement operator
   for(int i = 1; i <= 5; i++)
   {
      // inner for loop with increment operator
      for(int j = i; j >= 1; j--)
      {
         printf("*");
      }
      printf("\n");
   }

   return 0;
}

Star Pattern Program in C – 3

Sample input and outputs are given below,

N = 5
*****
****
***
**
*

N = 7
*******
******
*****
****
***
**
*

In this star pattern in C, for the input N, there are N rows. Therefore the outer loop will go from 1 to N. But the number of columns in a given row depends upon the row number. Example:- for input n = 5, in the 1st row there are 5 columns containing the star symbol, in the 2nd row there are 4 columns, in the 3rd row there are 3 columns, in the 4th row there are 2 columns, and in the 5th row there is only one column.

Hence in general, we can say the ith row contains (N – i + 1) columns. For example:- if input n = 7 then 1st row contains 7-1+1 = 7 stars, 3rd row contains 7-3+1 = 5 stars and so on. Therefore, the inner loop representing the column will go from 1 to (N – i + 1).

#include<stdio.h>
int main()
{
   int n;

   printf("Enter number of rows: ");
   scanf("%d", &n);

   for(int i = 1; i <= n; i++)
   {
      for(int j = 1; j <= (n-i+1); j++)
      {
         printf("*");
      }
      printf("\n");
   }

   return 0;
}

Output:-

Enter number of rows: 5
*****
****
***
**
*

In this star pattern program in C, we were using increment operators both inside the outer and inner loop. But we can also use decrements in the outer and inner loops. Below star program in C demonstrates it,

#include<stdio.h>
int main()
{
   int n;

   printf("Enter number of rows: ");
   scanf("%d", &n);

   for(int i = n; i >= 1; i--)
   {
      for(int j = i; j >= 1; j--)
      {
         printf("*");
      }
      printf("\n");
   }

   return 0;
}

Star Pattern Program in C – 4

Sample input and outputs are given below,

N = 5
    *
   **
  ***
 ****
*****
N = 7
      *
     **
    ***
   ****
  *****
 ******
*******

In this star pattern, star (*) isn’t the only character that should be displayed on the screen. It contains a combination of spaces and stars. For example:- when n = 5 then the 1st row contains 4 spaces and 1 star.

If we observe carefully then in this pattern, for input N there are N rows and N columns. For example:- when n = 5 then it contains 5 rows and 5 columns. Similarly, when n = 7 then it contains 7 rows and 7 columns. Therefore the outer loop representing the row will always go from 1 to N.

In this star pattern, columns are built by a combination of stars and spaces. When n = 5 then 1st row contains 5 columns among them 4 are spaces and 1 star, 2nd row contains 3 spaces and 2 stars, 3rd row contains 2 spaces and 3 stars, 4th row contains 1 space and 4 stars, 5th row contains 0 space only 5 stars. We can conclude that ith row contains N – i space and i star.

#include<stdio.h>
int main()
{
   int n;

   printf("Enter number of rows: ");
   scanf("%d", &n);

   for(int i = 1; i <= n; i++)
   {
      for(int s = 1; s <= n-i; s++) printf(" ");
      for(int j = 1; j <= i; j++) printf("*");
      printf("\n");
   }

   return 0;
}

This program contains 1 outer loop and 2 inner loops. The same program can be written using only one inner loop but we have to use an if-else statement to separate spaces with stars.

#include<stdio.h>
int main()
{
   int n;

   printf("Enter number of rows: ");
   scanf("%d", &n);

   for(int i = 1; i <= n; i++)
   {
      for(int j = 1; j <= n; j++)
      {
         if(j <= n-i) printf(" ");
         else printf("*");
      }
      printf("\n");
   }

   return 0;
}

You can also write several different C programs for the same star pattern by manipulating increment and decrement operators.

Can you tell the differences between the previous and below star patterns and write the program for it.

Sample input/output:-

N = 5
----*
---**
--***
-****
*****
N = 7
------*
-----**
----***
---****
--*****
-******
*******

Well, this pattern and the previous star pattern don’t contain too many differences. If you observe carefully then you will notice that spaces are replaced with ‘-’ characters, while all other things are exactly the same. Therefore in the previous program, just replace space with ‘-’ as follows:-

for(int i = 1; i <= n; i++)
{
   for(int j = 1; j <= n; j++)
   {
      if(j <= n-i) printf("-");
      else printf("*");
   }
   printf("\n");
}

Star Pattern Program in C – 5

Can you tell the differences between the previous (Star Pattern Program in C – 4) and below star pattern and write the program for it. 

Sample input/output:-

N = 5
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
N = 7
      * 
     * * 
    * * * 
   * * * * 
  * * * * * 
 * * * * * * 
* * * * * * * 

There is only one line difference between the previous star pattern containing stars with spaces, and the current star program.

In the previous star pattern in C,

printf("*"); // print star

In the current star pattern in C,

printf("* "); // print 1 star & 1 space

While displaying the star symbol, If we add one space after the star symbol then we will achieve this star pattern in C. Below star program in C demonstrate it,

#include<stdio.h>
int main()
{
   int n;

   printf("Enter number of rows: ");
   scanf("%d", &n);

   for(int i = 1; i <= n; i++)
   {
      for(int j = 1; j <= n; j++)
      {
         if(j <= n-i) printf(" ");
         else printf("* ");
         // print 1 star & 1 space.
      }
      printf("\n");
   }

   return 0;
}

Star Pattern Program in C – 6

Sample input/output:-

N = 5
*****
 ****
  ***
   **
    *
N = 7
*******
 ******
  *****
   ****
    ***
     **
      *

It is the reverse of the Star Pattern Program in C – 4. In this pattern also, the number of rows is equal to the input size N. For input n=5, there are 5 rows and similarly, for input n = 7, there are 7 rows. Therefore the outer loop will go from 1 to N.

In the ith row, there are N columns among them (i-1) columns containing space and the remaining columns containing stars. For example:- when input n = 5 then 1st row contains 1-1=0 space and remaining are stars. The 2nd row contains 2-1=1 space and the remaining are stars, and so on.

#include<stdio.h>
int main()
{
   int n;

   printf("Enter number of rows: ");
   scanf("%d", &n);

   for(int i = 1; i <= n; i++)
   {
      for(int j = 1; j <= n; j++)
      {
         if(j < i) printf(" ");
         else printf("*");
      }
      printf("\n");
   }

   return 0;
}

Star Pattern Program in C – 7

Sample input/output:-

N = 5
* * * * * 
 * * * * 
  * * * 
   * * 
    * 
N = 7
* * * * * * * 
 * * * * * * 
  * * * * * 
   * * * * 
    * * * 
     * * 
      * 

In this star pattern, there is only one difference compared to the previous star pattern program in C. While displaying a star it also contains one space. Below star program in C demonstrate it:-

#include<stdio.h>
int main()
{
   int n;

   printf("Enter number of rows: ");
   scanf("%d", &n);

   for(int i = 1; i <= n; i++)
   {
      for(int j = 1; j <= n; j++)
      {
         if(j < i) printf(" ");
         else printf("* ");
      }
      printf("\n");
   }

   return 0;
}

Full Pyramid Star Pattern in C

Star Pattern Program in C – 8 | Sample input/outputs of full pyramid star pattern in C:-

N = 5
    *
   ***
  *****
 *******
*********
N = 7
      *
     ***
    *****
   *******
  *********
 ***********
*************

In this full pyramid star pattern in C for the input N, there are N rows. Therefore the outer loop will go from 1 to N. Example:- when input n=5, there are 5 rows and similarly for input n = 7, there are 7 rows.

In the ith row, there is (N – i) space and then (2*i – 1) stars are there. For example:- when input n = 5 then in the 1st row 5-1 = 4 spaces are there and 2*1-1 = 1 star is available, in the 2nd row 5-2 = 3 spaces are there and 2*2-1 = 3 stars are available, and so on.

#include<stdio.h>
int main()
{
   int n;

   printf("Enter number of rows: ");
   scanf("%d", &n);

   for(int i = 1; i <= n; i++)
   {
      for(int s = 1; s <= n-i; s++) printf(" ");
      for(int j = 1; j <= (2*i-1); j++) printf("*");
      printf("\n");
   }

   return 0;
}

The above program contains 2 inners for loops to display space and stars separately but we can also do the same with only one inner loop and using if-else statements.

The inner loop will go from 1 to (N + i – 1) where n is input, and i is row-number. For example:- when input n = 5 then 1st row contains 5 + 1 – 1 = 5 columns, 2nd row contains 5 + 2 – 1 = 6 columns, and so on. 

In the ith row, there are (N – i) spaces and the remaining are stars. The below star program in C demonstrates it.

#include<stdio.h>
int main()
{
   int n;

   printf("Enter number of rows: ");
   scanf("%d", &n);

   for(int i = 1; i <= n; i++)
   {
      for(int j = 1; j <= (n + i - 1); j++) 
      {
         if( j <= n-i) printf(" ");
         else printf("*");
      }
      printf("\n");
   }

   return 0;
}

Full Pyramid Star Pattern on the Center of the Screen

Star Pattern Program in C – 9 | The previous full pyramid star pattern was on the left side of the screen. Let us develop another star pattern program in C to display a full pyramid star pattern at the center of the screen.

Note:- Generally, on a computer screen, we can print a maximum of 80 characters horizontally. 

#include<stdio.h>
int main()
{
   int n, c = 80;

   printf("Enter number of rows: ");
   scanf("%d", &n);

   for(int i = 1; i <= n; i++)
   {
      for(int j = 1; j < (c/2 + i); j++) 
      {
         if( j <= (c/2 - i)) printf(" ");
         else printf("*");
      }
      printf("\n");
   }

   return 0;
}

The output of the above program:-

Enter number of rows: 5
                                       *
                                      ***
                                     *****
                                    *******
                                   *********
Enter number of rows: 7
                                       *
                                      ***
                                     *****
                                    *******
                                   *********
                                  ***********
                                 *************

Or, it can be also written as below program,

#include<stdio.h>
int main()
{
   int n, c = 80;

   printf("Enter number of rows: ");
   scanf("%d", &n);

   for(int i=1;i<=n;i++)
   {
      for(int j=1;j<=(c/2-i);j++)
      {
         printf(" "); // blank space
      }

      for(int k=1;k<=(2*i-1);k++)
      {
         printf("*");
      }

      printf("\n");
   }

   return 0;
}

Inverted Full Pyramid Star Pattern in C

Star Pattern Program in C – 10 | Sample input/outputs of inverted full pyramid star pattern in C:-

Enter number of rows: 5
*********
 *******
  *****
   ***
    *
Enter number of rows: 7
*************
 ***********
  *********
   *******
    *****
     ***
      *

The C program for the above Inverted Full Pyramid Star Pattern is given below,

#include<stdio.h>
int main()
{
   int n, c = 80;

   printf("Enter number of rows: ");
   scanf("%d", &n);


   for(int i = 1; i <= n; i++)
   {
      for(int j = 1; j <= (2*n - i); j++) 
      {
         if( j < i ) printf(" ");
         else printf("*");
      }
      printf("\n");
   }

   return 0;
}

Other Star Pattern

Star Pattern Program in C – 11 | Sample input/outputs of inverted full pyramid star pattern in C:-

Enter number of rows: 5
 *        *
 **      **
 ***    ***
 ****  **** 
 **********
Enter number of rows: 7
 *            *
 **          **
 ***        ***
 ****      ****
 *****    *****
 ******  ******
 **************

C Program for the above-given pattern,

#include<stdio.h>
int main()
{
   int n, c = 80;

   printf("Enter number of rows: ");
   scanf("%d", &n);

   for(int i = 1; i <= n; i++)
   {
      for(int j = 1; j <= 2*n; j++) 
      {
         if( j <= i || j > (2*n-i)) printf("*");
         else printf(" ");
      }
      printf("\n");
   }

   // printf("program-2 \n");

   return 0;
}

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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