Here we will see how to print an inverted hollow star pyramid using a C program. Below are the examples:
Input: row = 5
Output:
*********
* *
* *
* *
*Input: row = 7
Output:
*************
* *
* *
* *
* *
* *
*
There are 2 ways to print an inverted hollow star pyramid in C:
- Using For Loop.
- Using While Loop.
Let’s start discussing each of these methods in detail.
Using For Loop
Approach:
The approach is to use three loops:
- One is to control the number of rows.
- The second is to control the blank spaces.
- The third is to control the number of columns.
Then by using concepts of the nested loop one can easily print the pattern.
Algorithm:
- Take the Input of row value for the Inverted Pyramid.
- We need to use three loops one is the outer loop to change the line and two inner loops one to print the star and the other to print the space.
- The outer loop iterate row times and print a newline after completing the inner loop.
- The first inner loop is used to print the space from 1 to (current_row -1).
- The second inner loop is used to print the star ( “*” ) at first and last column (i.e length _of_row * 2 – (2 * Current_row – 1)) .
- The second loop iterates from 1 to the last column.
Below is the C program to print an inverted hollow star pyramid using for loop:
// C program to print an inverted hollow // star pyramid using for loop #include <stdio.h> void pattern_fun( int row)
{ // To iterate through the rows
for ( int j = 1; j <= row; j++)
{
// To print the beginning spaces
for ( int sp = 1; sp <= j - 1; sp++)
{
printf ( " " );
}
// Iterating from jth column to
// last column (row*2 - (2*j - 1))
int last_col = (row * 2 - (2 * j - 1));
// To iterate through column
for ( int k = 1; k <= last_col; k++)
{
// To Print all star for first
// row (j==1) jth column (k==1)
// and for last column
// (row*2 - (2*j - 1))
if (j == 1 || k == 1)
printf ( "*" );
else if (k == last_col)
printf ( "*" );
else
printf ( " " );
}
// Proceeding to next row.
printf ( "\n" );
}
} // Driver code int main()
{ // Number of rows
int row = 7;
// Calling the function to
// print the pattern.
pattern_fun(row);
return 0;
} |
************* * * * * * * * * * * *
- Time Complexity: O(n2), as the nested loop, is used.
- Auxiliary Space: O(1), no extra space is required, so it is a constant.
Using While Loop:
Below is the C program to print an inverted hollow star pyramid using a while loop:
// C program to print an inverted // hollow star pyramid using while // loop #include <stdio.h> void pattern_fun( int row)
{ // Declaring and initializing
// the loop control variables.
int j = 1;
int sp = 1;
int k = 1;
// To iterate through the rows
while (j <= row)
{
// Initializing the space
// control variable.
sp = 1;
// To print the beginning spaces
while (sp <= j - 1)
{
printf ( " " );
// Incrementing the value
sp++;
}
// Iterating from jth column to
// last column (row*2 - (2*j - 1))
int last_col = (row * 2 - (2 * j - 1));
// Initializing the column control
// variable.
k = 1;
// To iterate through column
while (k <= last_col)
{
// To Print all star for first
// row (j==1) jth column (k==1)
// and for last column
// (row*2 - (2*j - 1))
if (j == 1 || k == 1)
printf ( "*" );
else if (k == last_col)
printf ( "*" );
else
printf ( " " );
// Incrementing the value
k++;
}
// Proceeding to next row.
printf ( "\n" );
// Incrementing the value
j++;
}
} // Driver code int main()
{ // Number of rows
int row = 4;
// Calling the function to
// print the pattern.
pattern_fun(row);
return 0;
} |
******* * * * * *
- Time Complexity: O(n2), as the nested loop is used.
- Auxiliary Space: O(1), no extra space is required, so it is a constant.