Pages

Friday, October 9, 2015

Pattern : C program to print Hollow Square for given value N.

This program will print hollow square pattern.
 
#include<stdio.h>
int main()
{
 int i,j,n=5;
 //for loop to print line 1
 for(i=0; i<n;i++)
 {
  printf("* ");
 }
 printf("\n");
 
 // logic for repeating line 2 to 4, n-2 times
 for(j=0; j<n-2; j++)
 {
 //for loop to print *    * line 2 to 4
  printf("* ");
 
  for(i=0; i<n-2; i++)
   printf("  ");
 
  printf("*\n");
 }
 for(i=0; i<n;i++)
 {
  printf("* ");
 }
  return 0;
}

Output of the program

* * * * *
*       *
*       *
*       *
* * * * *

1 comment:

  1. This programs will be useful to beginners of C language students.

    ReplyDelete