Pyramid Star Pattern in C

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

There are many pattern programs are written by programmers for practice purposes. The pyramid star pattern in C is one of them. In this post, we will display half pyramid star pattern, inverted half pyramid star pattern, full pyramid star pattern, full pyramid star pattern at the center of the screen, and inverted full pyramid star pattern.

1. The Half pyramid star pattern

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

Below code display Half pyramid star pattern,

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

  // outer loop represents row
  for(int i=1;i<=5;i++)
  {
   
    // inner loop represents column
    for(int j=1;j<=i;j++)
    {
      printf("* "); // star
    }

    printf("\n"); // new line
  }

  return 0;
}

The previous program uses the increment operator to display the Half pyramid star pattern in C. But we can also use the decrement operator.

Half pyramid star pattern using both increment and decrement operators,

Here we will use the decrement operator for the outer loop and the increment operator for the inner loop to print the above print.

#include<stdio.h>
int main()
{
  // outer for loop with decrement operator
  for(int i=5; i>=1; i--)
  {
    // inner for loop with increment operator
    for(int j=i; j<=5; j++)
    {
      printf("* "); // star
    }

    printf("\n"); // new line
  }

  return 0;
}

Now, we will use the increment operator for the outer loop and decrement operator for the inner loop to print the same (above) pattern.

#include<stdio.h>
int main()
{
  // outer for loop with increment operator
  for(int i=1; i<=5; i++)
  {
    // inner for loop with decrement operator
    for(int j=i; j>=1; j--)
    {
      printf("* "); // star
    }

    printf("\n"); // new line
  }

  return 0;
}

2. The Inverted half pyramid star pattern

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

The code for the Inverted half pyramid star pattern is,

#include<stdio.h>
int main()
{
  // outer for loop
  for(int i=5; i>=1; i--)
  {
    // inner for loop
    for(int j=i; j>=1; j--)
    {
      printf("* "); // star
    }

    printf("\n"); // new line
  }

  return 0;
}

3. Write a C program to display the below star pattern

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

The code for the above pattern is given below,

#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; j++)
     printf(" "); // space 
     for(int k=1; k <= i; k++)
     printf("*"); // star

     printf("\n"); // new line
   }

   return 0;
}

4. Write a C program to display the below star pattern

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

The code for the above pattern in C is,

#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(" "); // space 
      else
      printf("*"); // star
    }
    printf("\n"); // new line
  }
  
  return 0;
}

Full Pyramid Star Pattern in C

5. Write a C program to print Full pyramid star pattern

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

Full star pattern program in C is given below,

#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; j++)
    printf(" "); // space 

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

    printf("\n"); // new line
  }

  return 0;
}

There are many another ways to solve this problem,

#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< 2*n; j++)
    {
        if(j<= n-i)
        printf(" "); // spaces
        
        else if(j < n+i)
        printf("*"); //stars

    }
    printf("\n"); //next line
  }
  return 0;
}

The same pattern also can be displayed using the recursion.

#include<stdio.h>
void fullPyramid (int r)
{
   int c, space;
   static int stars = -1;

   if (r <= 0) return;

   space = r - 1;
   stars += 2;

   for (c = 0; c < space; c++)
   printf(" "); // space
   
   for (c = 0; c < stars; c++)
   printf("*"); // star

   printf("\n");

   fullPyramid(--r);
}

int main ()
{
  int n;
  printf("Enter number of rows: ");
  scanf("%d",&n);
  fullPyramid(n);
  return 0;
}

6. Full pyramid star pattern at the center of the screen

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

Generally, on a computer screen, we can print Maximum 80 characters horizontally. Here we will print the full pyramid for n lines.

#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(" "); // space

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

     printf("\n"); //next line
   }

   return 0;
}

Inverted Full Pyramid Star Pattern

7. Write a program to print inverted full pyramid star pattern.

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

Solution-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< 2*n; j++)
    {
        if(j<i) 
        printf(" "); // space

        else if(j <= 2*n-i) 
        printf("*"); //star
    }
    
    printf("\n"); // new line
  }

  return 0;
}

Solution-2,

#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=1; j <= n-i; j++)
    printf(" "); // space
    
    for(int k=1; k <= (2*i-1); k++)
    printf("*"); //star
    
    printf("\n"); // new line
  }

  return 0;
}

Solution-3,

#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=n; j>i; j--)
    printf(" "); //space
    
    for(int k=1; k<=2*i-1; k++)
    printf("*"); //star
    
    printf("\n"); //next line
  }

  return 0;
}

8. Write a C program to display the below star pattern

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

Code for the above pattern is,

#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; j++)
    printf(" "); // spaces
    
    for(int k=1; k <= i; k++)
    printf("* "); //stars with space
    
    printf("\n"); // new line
  }

  return 0;
}

9. Write a C program to display the below star pattern

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

The code for the above pattern is, 

#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 <= 2*n; j++)
    {
        if(j <= i || j > (2*n-i) ) 
        printf("*"); // star
        else 
        printf(" "); // space
    }

    printf("\n"); // new line
  }

  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 do you find anything incorrect? Let us know in the comments. Thank you!

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 among 3 Numbers
Find Grade of student
Find the absolute value
Check 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

Leave a Comment

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