C program to print the hollow stars square

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.

#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.
star

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.

Reactions:

9 comments:

  1. Thank you soo much!!
    You've helped me a lot!! >3<
    haha..

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Thank you so much! Really helpful for newbie programmers up to this day :)

    ReplyDelete
  4. i want to code a rectangle of stars within a rectangle of stars can anybody help me thanks?

    ReplyDelete
  5. Please help me in coding of diamond with hollow center and many stars around the center

    ReplyDelete
  6. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from

    ReplyDelete
  7. Java jobs are becoming more plentiful as the use of this software is more and more common online. Those skilled in java applications are in demand now by telecommuting companies. These jobs allow you to work from home with java software. See more assignment programming

    ReplyDelete

  8. I want these code in Java Can you give me code ?


    #
    ###
    #####
    ##
    #


    How to Write this program to Print Star Pattern in Java

    ReplyDelete