C

Binary Layers – Diamond Cut Pattern

50
0
Home C Binary Layers – Diamond Cut Pattern

The program must accept an integer N as the input. The program must
print the desired pattern as shown in the Example Input/Output section.

Boundary Condition(s):
1 <= N <= 100

Input Format:
The first line contains N.
Output Format:
The lines contain the desired pattern as shown in the Example Input/Output section.

Example Input/Output 1:
Input:

5
Output:
####1
###000
##11111
#0100010
101010101
#0100010
##11111
###000
####1
Explanation:
The binary layers pattern for 5 is given below.
111111111
100000001
101111101
101000101
101010101
101000101
101111101
100000001
111111111
Here, the diamond part of the above pattern is printed as the output.

Example Input/Output 2:
Input:

4
Output:
###0
##111
#10001
0101010
#10001
##111
###0

#include<stdio.h>

int main()
{
	int n;
	scanf("%d", &n);
	int t=n;
	int ml=(n*2)-1;
	int h=ml;
	int x,y;
	int b[ml][ml],v,k=n-1,f=n-1;
	for(int i=0;i<=ml;i++)
	{
		if(n%2!=0)
			v=(i%2==0)?1:0;
		else
			v=(i%2==0)?0:1;
		for(int j=i;j<ml-i;j++)
		{
			b[i][j]=v;
			b[j][i]=v;
			b[ml-i-1][j]=v;
			b[j][ml-i-1]=v;
		}
	}
	int u=1;
	x=n-1;
	y=n-1;
	for(int i=0;i<=ml/2;i++)
	{
	    for(int j=0;j<n-i-1;j++)
	        printf("#");
	    for(int j=x;j<=y;j++)
	    {
	        printf("%d",b[i][j]);
	    }
	    x-=1;
	    y+=1;
	    printf("\n");
	}
	x=1;
	y=ml-1;
	for(int i=0;i<ml/2;i++)
	{
	    for(int j=0;j<=i;j++)
	    {
	        printf("#");
	    }
	    for(int j=x;j<y;j++)
	    {
	        printf("%d",b[n-i][j]);
	    }
	    x+=1;
	    y-=1;
	    printf("\n");
	}
}
Hephzibai
WRITTEN BY

Hephzibai

Hephzibai is a driven third-year student at St. Joseph's Institute of Technology, specializing in Computer Science and Engineering. With a keen interest in data science and also in fullstack.
One of my greatest strengths lies in my programming skills, which I've honed through countless hours of practice and participation in coding challenges. Platforms like Skillrack, HackerRank, and others have been my playgrounds, where I've tackled a wide range of problems with creativity and determination.

Leave a Reply

Your email address will not be published. Required fields are marked *