C Program to print Hollow Diamond inside Square Pattern for n number of lines.[Pattern 22]| Using C language| C Program series 43|
👨💻C Program to print Hollow Diamond inside Square for n number of lines.[Pattern:22]
Code :
For n number of LINES:
/*
C Program to print Hollow Diamond inside Square Pattern for n number of lines.[Pattern 22]
For Example:n=9
#########
#### ####
### ###
## ##
# #
## ##
### ###
#### ####
#########
*/
#include<stdio.h>
main()
{
printf("C Program to print Hollow Diamond inside Square Pattern for n number of lines.[Pattern 22]\n\n");
int i,j,n;
printf("Enter the ODD number of lines:");
scanf("%d",&n);
if(n%2==0)
{
n=n+1;
}
printf("Pattern for %d lines.\n\n\t",n);
int half=n/2;
for(i=1;i<=n;i++)
{
if(i==1 || i==n)
{
for(j=1;j<=n;j++)
printf("#");
}
else
{
if(i<=half+1)
{
for(j=half+2-i;j>0;j--)
printf("#");
for(j=1;j<=i-1;j++)
printf(" ");
for(j=2;j<=i-1;j++)
printf(" ");
for(j=half+2-i;j>0;j--)
printf("#");
}
else
{
for(j=1;j<=i-half;j++)
printf("#");
for(j=n-i;j>0;j--)
printf(" ");
for(j=n-i-1;j>0;j--)
printf(" ");
for(j=1;j<=i-half;j++)
printf("#");
}
}
printf("\n\t");//here, '\t' used to shift pattern 1 tab space right side
}
}
The output of Code:
Comments
Post a Comment