[2000 character limit] In this question you are going write another program that prints a pyramid similar to that in Q4, but in negative space: Imagine you have a rectangle drawn by +'s and cut a hole of the size of the pyramid, leaving each side with just one + character (see sample input/outputs for details).
Program:-
#include <stdio.h>
int main() {
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
if(rows<=0){
printf("Invalid base size:Has to be positive int\n");
return 0;
}
for (i = 0; i <= rows+1; ++i, k = 0) {
//to print first and last lines
if(i==0 || i==rows+1){
for(int j=0;j<(2*rows)+1;j++){
printf("+");
}
printf("\n");
continue;
}
//to print first half '+'
for (space = 0; space <= rows - i; ++space) {
printf("+");
}
//to print spaces
while (k != 2 * i - 1) {
printf(" ");
++k;
}
//to print second half '+'
for (space = 0; space <= rows - i; ++space) {
printf("+");
}
printf("\n");
}
return 0;
}
Step by step
Solved in 2 steps