C Program For Printing 180 Degree Rotation of Simple Half Left Pyramid
Last Updated :
10 Aug, 2022
We can print the 180-degree rotation of simple half-left pyramid using for and while loop as follows:
Input:
rows = 5
Output:
*
**
***
****
*****
Approach 1:
Using for loop:
Explanation: First for loop is used to identify the number of rows and the second for loop is used to identify the number of columns. Here the values will be changed according to the first for loop. If j is greater than i then it will print the output otherwise print the space.
C
#include <stdio.h>
int main()
{
int rows = 5;
for ( int i = rows; i > 0; i--) {
for ( int j = 0; j <= rows; j++) {
if (j >= i) {
printf ( "*" );
}
else {
printf ( " " );
}
}
printf ( "\n" );
}
return 0;
}
|
Output
*
**
***
****
*****
Approach 2:
Using while loop
Explanation: The while loops check the condition until the condition is false. If condition is true then enters in to loop and execute the statements.
C
#include <stdio.h>
int main()
{
int i = 0, j = 0, sp = 0;
int rows = 5;
while (i < rows) {
while (sp < (rows - i - 1)) {
printf ( " " );
sp++;
}
sp = 0;
while (j <= i) {
printf ( "* " );
j++;
}
j = 0;
i++;
printf ( "\n" );
}
return 0;
}
|
Output
*
* *
* * *
* * * *
* * * * *
Time complexity: O(n2) for given input n rows
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...