Star Pattern Programs in C

Problem Description

Create star pattern programs in C programming upon receiving number of rows as input. star patterns like rhombus star pattern, hollow star pyramid pattern, plus star pattern, star patterns using while loop and functions.

What is Star pattern in C?

A star pattern is a pattern that shows up as a staircase of stars. It is a very common pattern in programming.

Example:
Input: – 5

Output:

*
**
***
****
*****
Problem Solution

There are various ways to print a star pattern programs in C language. Let’s take a detailed look at all the approaches to print a star pattern in C.

advertisement
advertisement
Method 1: Star Pattern Programs in C using For Loop

In this approach, we will use a nested for loop to print the stars.

Program/Source Code

Here is source code of the C Program to print star pattern using nested for Loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print Star Pattern using Nested For Loop
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main(void)
  8. {
  9.     int i, j, n;
  10.     printf("Enter the number of rows: ");
  11.     scanf("%d", &n);
  12.     for (i = 1; i <= n; i++)
  13.     {
  14.         for (j = 1; j <= i; j++)
  15.         {
  16.             printf("*");
  17.         }
  18.         printf("\n");
  19.     }
  20.     return 0;
  21. }
Program Explanation

1. Take the number of rows as input and store in the variable n.
2. In this program, we are using two for loops.
3. The outer for loop is used to print the rows and the inner for loop is used to print the stars in each row.
4. The outer for loop is executed n times where n is the number of rows.
5. The inner for loop is executed i times where i is the number of stars in each row.

Time Complexity: O(n2)
The time complexity of star pattern program is O(n2). The outer for loop is executed n times and the inner for loop is executed i times where i is the number of stars in each row. So the time complexity is O(2).

Space Complexity: O(1)
The space complexity of star pattern program in C is O(1). No extra space is used.

Run Time Testcases

In this case, we enter the number of rows as “5” to print the pattern with stars.

./star-pattern
 
Enter the number of rows: 5
*
**
***
****
*****

advertisement
Method 2: Star Pattern in C using While Loop

In this approach, we will use while loop to print the stars.

Program/Source Code

Here is source code of the C Program to print star pattern using while Loop. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print Star Pattern using While Loop
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main(void)
  8. {
  9.     int i = 1, j, n;
  10.     printf("Enter the number of rows: ");
  11.     scanf("%d", &n);
  12.     while (i <= n)
  13.     {
  14.         j = 1;
  15.         while (j <= i)
  16.         {
  17.             printf("*");
  18.             j++;
  19.         }
  20.         printf("\n");
  21.         i++;
  22.     }
  23.     return 0;
  24. }
Program Explanation

1. Take the number of rows as input and store in the variable n.
2. In this program, we are using two while loops.
3. The outer while loop is used to print the rows and the inner while loop is used to print the stars in each row.
4. The outer while loop is executed n times where n is the number of rows.
5. The inner while loop is executed i times where i is the number of stars in each row.

Time Complexity: O(n2)
The time complexity of star pattern program is O(n2). The outer for loop is executed n times and the inner for loop is executed i times where i is the number of stars in each row. So the time complexity is O(2).

advertisement

Space Complexity: O(1)
The space complexity of star pattern program in C is O(1). No extra space is used.

Run Time Testcases

In this case, we enter the number of rows as “3” to print the star pattern.

./star-pattern
 
Enter the number of rows: 3
*
**
***

Method 3: Star Pattern in C using Function

In this approach, we will make use of functions to print the stars.

Program/Source Code

Here is source code of the C Program to print stars using Function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print Stars using Function
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. void printN(char ch, int n)
  8. {
  9.     int i;
  10.     for (i = 1; i <= n; i++)
  11.     {
  12.         printf("%c", ch);
  13.     }
  14. }
  15.  
  16. int main(void)
  17. {
  18.     int i, n;
  19.     printf("Enter the number of rows: ");
  20.     scanf("%d", &n);
  21.     for (i = 1; i <= n; i++)
  22.     {
  23.         printN('*', i);
  24.         printf("\n");
  25.     }
  26.     return 0;
  27. }

Methods used:
printN(char ch, int n): This function prints the character ch n times.

Program Explanation

1. Take the number of rows as input and store in the variable n.
2. In this program, we are using a function to print the stars.
3. The function printN(char ch, int n) prints the character ch n times.
4. The outer for loop is executed n times where n is the number of rows.
5. The inner for loop is executed i times where i is the number of stars in each row.

Time Complexity: O(n2)
The time complexity of star pattern program is O(n2). The outer for loop is executed n times and the inner for loop is executed i times where i is the number of stars in each row. So the time complexity is O(2).

Space Complexity: O(1)
The space complexity of star pattern program in C is O(1). No extra space is used.

Run Time Testcases

In this case, we enter the number of rows as “7” to print the star pattern.

./star-pattern
 
Enter the number of rows: 7
*
**
***
****
*****
******
*******

Method 4: Rhombus Star Pattern in C

In this approach, we will print a rhombus star pattern.

Example:

input:
– 5

output:
*
***
*****
***
*

Program/Source Code

Here is source code of the C Program to print rhombus star pattern. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print Rhombus Star Pattern
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. void printN(char ch, int n)
  8. {
  9.     int i;
  10.     for (i = 1; i <= n; i++)
  11.     {
  12.         printf("%c", ch);
  13.     }
  14. }
  15.  
  16. int main(void)
  17. {
  18.     int i, n;
  19.     printf("Enter the number of rows: ");
  20.     scanf("%d", &n);
  21.     for (i = 1; i <= (n + 1)/2; i++)
  22.     {
  23.         printN(' ', n - i);
  24.         printN('*', 2 * i - 1);
  25.         printf("\n");
  26.     }
  27.  
  28.     for(i = (n + 1)/2; i <= n; i ++)
  29.     {
  30.         printN(' ', i - 1);
  31.         printN('*', 2 * (n - i) + 1);
  32.         printf("\n");
  33.     }
  34.     return 0;
  35. }
Program Explanation

1. Take the number of rows as input and store in the variable n.
2. In this program, we are using a function to print the spaces and stars.
3. The function printN(char ch, int n) prints the character ch n times.
4. In the main function inside the first for loop, i goes from 1 to (n + 1)/2. Here, we are printing the upper half of the rhombus.
5. For every iteration, there are n – i spaces and 2 * i – 1 stars. For n = 5, this will be 5 – i = 4, 3, and 2 spaces and 2 * i – 1 = 1, 3, and 5 stars.
6. In the second for loop, i goes from (n + 1)/2 to n. Here, we are printing the lower half of the rhombus.
7. For every iteration, there are i – 1 spaces and 2 * (n – i) + 1 stars. For n = 5, this will be i – 1 = 2, 3 and 4 spaces and 2 * (n – i) + 1 = 3 and 1 stars.

Time Complexity: O(n2)
The time complexity of this program is O(2). For every iteration, the function printN(char ch, int n) is executed n times. So the time complexity is O(2).

Space Complexity: O(1)
The space complexity of star pattern program in C is O(1). No extra space is used.

Run Time Testcases

Testcase 1: In this case, we enter the number of rows as “5” to print the rhombus star pattern.

./rhombus-star-pattern
 
Enter the number of rows: 5
    *
   ***
  *****
   ***
    *

Testcase 2: In this case, we enter the number of rows as “10” to print the rhombus star pattern.

./rhombus-star-pattern
Enter the number of rows: 10
         *
        ***
       *****
      *******
     *********
    ***********
     *********
      *******
       *****
        ***
         *

Method 5: Hollow Star Pyramid Pattern in C

In this approach, we will print a hollow star pyramid pattern.

Example:

Input:
– 5

Output:
*
* *
* *
* *

Program/Source Code

Here is source code of the C Program to print hollow star pyramid pattern. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print Hollow Star Pyramid Pattern
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. void printN(char ch, int n)
  8. {
  9.     int i;
  10.     for (i = 1; i <= n; i++)
  11.     {
  12.         printf("%c", ch);
  13.     }
  14. }
  15.  
  16. int main(void)
  17. {
  18.     int i, n;
  19.     printf("Enter the number of rows: ");
  20.     scanf("%d", &n);
  21.     for (i = 1; i <= n; i++)
  22.     {
  23.         printN(' ', n - i);
  24.         printN('*', 1);
  25.         if (i > 1)
  26.         {
  27.             printN(' ', 2 * i - 3);
  28.             printN('*', 1);
  29.         }
  30.         printf("\n");
  31.     }
  32.     return 0;
  33. }
Program Explanation

1. Take the number of rows as input and store in the variable n.
2. In this program, we are using a function to print the spaces and stars.
3. The function printN(char ch, int n) prints the character ch n times.
4. In the main function, the outer for loop is executed n times where n is the number of rows.
5. The inner if statement checks if i is greater than 1. If it is, then it prints the spaces and stars. For n = 5, this will be 2 * i – 3 = -1, 1, 3, and 5 spaces and 1 star every time.

Time Complexity: O(n2)
The time complexity of this program is O(n2). For every iteration, the function printN(char ch, int n) is executed n times. So the time complexity is O(n2).

Space Complexity: O(1)
The space complexity of star pattern program in C is O(1). No extra space is used.

Run Time Testcases

Testcase 1: In this case, we enter the number of rows as “5” to print the hollow star pyramid pattern.

./hollow-pyramid 
Enter the number of rows: 5
    *
   * *
  *   *
 *     *
*       *

Testcase 2: In this case, we enter the number of rows as “10” to print the hollow star pyramid pattern.

./hollow-pyramid
Enter the number of rows: 10
         *
        * *
       *   *
      *     *
     *       *
    *         *
   *           *
  *             *
 *               *

Method 6: Plus Star Pattern in C

In this approach, we will print a plus star pattern.

Example:

Input: – 5

Output:
*
*
*
*
*
***********
*
*
*
*
*

Program/Source Code

Here is source code of the C Program to print plus star pattern. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Print Plus Star Pattern
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. void printN(char ch, int n)
  8. {
  9.     int i;
  10.     for (i = 1; i <= n; i++)
  11.     {
  12.         printf("%c", ch);
  13.     }
  14. }
  15.  
  16. int main(void)
  17. {
  18.     int i, n;
  19.     printf("Enter the number of rows: ");
  20.     scanf("%d", &n);
  21.     for (i = 1; i <= n; i++)
  22.     {
  23.         printN(' ', n);
  24.         printN('*', 1);
  25.         printf("\n");
  26.     }
  27.     printN('*', 2 * n + 1);
  28.     printf("\n");
  29.     for (i = 1; i <= n; i++)
  30.     {
  31.         printN(' ', n);
  32.         printN('*', 1);
  33.         printf("\n");
  34.     }
  35.     return 0;
  36. }
Program Explanation

1. Take the number of rows as input and store in the variable n.
2. In this program, we are using a function to print the spaces and stars.
3. The function printN(char ch, int n) prints the character ch n times.
4. The first for loop goes from 1 to n. Here, we are printing the first half of the plus. For every iteration, there are n spaces and 1 star. For n = 5, this will be 5 spaces and 1 star.
5. In the next line we print 2 * n + 1 stars. For n = 5, this will be 2 * 5 + 1 = 11 stars.
6. The second for loop goes from 1 to n. Here, we are printing the second half of the plus. For every iteration, there are n spaces and 1 star. For n = 5, this will be 5 spaces and 1 star.

Time Complexity: O(n2)
The time complexity of this program is O(n2). For every iteration, the function printN(char ch, int n) is executed n times. So the time complexity is O(n2).

Space Complexity: O(1)
The space complexity of star pattern program in C is O(1). No extra space is used.

Run Time Testcases

Testcase 1: In this case, we enter the number of rows as “5” to print plus star pattern.

./plus-star
Enter the number of rows: 5
     *
     *
     *
     *
     *
***********
     *
     *
     *
     *
     *

Testcase 2: In this case, we enter the number of rows as “10” to print plus star pattern.

./plus-star      
Enter the number of rows: 10
          *
          *
          *
          *
          *
          *
          *
          *
          *
          *
*********************
          *
          *
          *
          *
          *
          *
          *
          *
          *
          *

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.