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.
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:
* ** *** **** *****
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.
- Star Pattern in C using For Loop
- Star Pattern in C using While Loop
- Star Pattern Programs in C using Function
- Rhombus Star Pattern in C
- Hollow Star Pyramid Pattern in C
- Plus Star Pattern in C
In this approach, we will use a nested for loop to print the stars.
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.
/*
* C Program to Print Star Pattern using Nested For Loop
*/
#include <stdio.h>
int main(void)
{
int i, j, n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
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.
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 * ** *** **** *****
In this approach, we will use while loop to print the stars.
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.
/*
* C Program to Print Star Pattern using While Loop
*/
#include <stdio.h>
int main(void)
{
int i = 1, j, n;
printf("Enter the number of rows: ");
scanf("%d", &n);
while (i <= n)
{
j = 1;
while (j <= i)
{
printf("*");
j++;
}
printf("\n");
i++;
}
return 0;
}
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).
Space Complexity: O(1)
The space complexity of star pattern program in C is O(1). No extra space is used.
In this case, we enter the number of rows as “3” to print the star pattern.
./star-pattern Enter the number of rows: 3 * ** ***
In this approach, we will make use of functions to print the stars.
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.
/*
* C Program to Print Stars using Function
*/
#include <stdio.h>
void printN(char ch, int n)
{
int i;
for (i = 1; i <= n; i++)
{
printf("%c", ch);
}
}
int main(void)
{
int i, n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printN('*', i);
printf("\n");
}
return 0;
}
Methods used:
printN(char ch, int n): This function prints the character ch n times.
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.
In this case, we enter the number of rows as “7” to print the star pattern.
./star-pattern Enter the number of rows: 7 * ** *** **** ***** ****** *******
In this approach, we will print a rhombus star pattern.
Example:
input:
– 5
output:
*
***
*****
***
*
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.
/*
* C Program to Print Rhombus Star Pattern
*/
#include <stdio.h>
void printN(char ch, int n)
{
int i;
for (i = 1; i <= n; i++)
{
printf("%c", ch);
}
}
int main(void)
{
int i, n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = 1; i <= (n + 1)/2; i++)
{
printN(' ', n - i);
printN('*', 2 * i - 1);
printf("\n");
}
for(i = (n + 1)/2; i <= n; i ++)
{
printN(' ', i - 1);
printN('*', 2 * (n - i) + 1);
printf("\n");
}
return 0;
}
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.
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 * *** ***** ******* ********* *********** ********* ******* ***** *** *
In this approach, we will print a hollow star pyramid pattern.
Example:
Input:
– 5
Output:
*
* *
* *
* *
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.
/*
* C Program to Print Hollow Star Pyramid Pattern
*/
#include <stdio.h>
void printN(char ch, int n)
{
int i;
for (i = 1; i <= n; i++)
{
printf("%c", ch);
}
}
int main(void)
{
int i, n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printN(' ', n - i);
printN('*', 1);
if (i > 1)
{
printN(' ', 2 * i - 3);
printN('*', 1);
}
printf("\n");
}
return 0;
}
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.
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 * * * * * * * * * * * * * * * * *
In this approach, we will print a plus star pattern.
Example:
Input: – 5
Output:
*
*
*
*
*
***********
*
*
*
*
*
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.
/*
* C Program to Print Plus Star Pattern
*/
#include <stdio.h>
void printN(char ch, int n)
{
int i;
for (i = 1; i <= n; i++)
{
printf("%c", ch);
}
}
int main(void)
{
int i, n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printN(' ', n);
printN('*', 1);
printf("\n");
}
printN('*', 2 * n + 1);
printf("\n");
for (i = 1; i <= n; i++)
{
printN(' ', n);
printN('*', 1);
printf("\n");
}
return 0;
}
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.
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]- Apply for C Internship
- Check C Books
- Practice BCA MCQs
- Check Computer Science Books
- Practice Computer Science MCQs