Here is the code for the C program to print the star formation of a hollow square. The program will input an integer value from the user and print the hollow star formations.
Here is the code.
Here is the sample output.
As can be seen in the output above, for the user input, the hollow star square will be formed. The key to understand the logic of the code is as follows.
The outerloop controls the number of lines and inner loop controls the number of stars.
Here is the code.
#include “stdio.h” int main() { int num,i,j; printf("Please enter a number to print the hollow star formation \n"); scanf("%d", &num); for(i=0;i<num;i++) { for(j=0;j<num;j++) { if(i==0 || i==num-1) { printf("*"); } else if(j==0 || j==num-1) { printf("*"); } else { printf(" "); // space is printed .. } } printf("\n"); } return 0; } |
Here is the sample output.
As can be seen in the output above, for the user input, the hollow star square will be formed. The key to understand the logic of the code is as follows.
The outerloop controls the number of lines and inner loop controls the number of stars.