C Program to Print 180 Degree Rotation of Inverted Half Pyramid
Last Updated :
04 Aug, 2022
C program to print 180-degree rotation of inverted half pyramid pattern using two approaches i.e. for loop and while loop.
Input: Number = 5
Output:
* * * * *
* * * *
* * *
* *
*
Approach 1: Using for Loop
The first for loop is used to identify the number of rows, the second for loop is used to handle the spaces and the third for loop is used to identify the number of columns. Here the values will be changed according to the first for loop, then print the required pattern.
C
#include <stdio.h>
int main()
{
int number = 5;
int k = 2 * number - 2;
for ( int rows = number; rows > 0; rows--) {
for ( int columns = 0; columns < number - rows;
columns++)
printf ( " " );
k = k - 2;
for ( int columns = 0; columns < rows; columns++) {
printf ( "* " );
}
printf ( "\n" );
}
return 0;
}
|
Output
* * * * *
* * * *
* * *
* *
*
Time Complexity: O(n2)
Auxiliary Space: O(1)
Approach 2: Using while Loop
The while loops check the condition until the condition is false. If the condition is true then enter into the loop and execute the statements.
C
#include <stdio.h>
int main()
{
int number = 5;
int rows = number, columns = 0, k = 0;
while (rows > 0) {
while (k < (number - rows)) {
printf ( " " );
k++;
}
k = 0;
while (columns < rows) {
printf ( "* " );
columns++;
}
columns = 0;
rows--;
printf ( "\n" );
}
return 0;
}
|
Output
* * * * *
* * * *
* * *
* *
*
Time Complexity: O(n2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...