Pattern Printing - Diamond Numbers
Given an integer N as the input, print the pattern as given in the Example Input/Output section.
Input Format:
The first line contains N.
The first line contains N.
Output Format:
2N-1 lines containing the desired pattern.
2N-1 lines containing the desired pattern.
Boundary Conditions:
2 <= N <= 50
2 <= N <= 50
Example Input/Output 1:
Input:
3
Input:
3
Output:
0 0 1 0 0
0 2 0 8 0
3 0 0 0 7
0 4 0 6 0
0 0 5 0 0
0 0 1 0 0
0 2 0 8 0
3 0 0 0 7
0 4 0 6 0
0 0 5 0 0
Example Input/Output 2:
Input:
5
Input:
5
Output:
0 0 0 0 1 0 0 0 0
0 0 0 2 0 16 0 0 0
0 0 3 0 0 0 15 0 0
0 4 0 0 0 0 0 14 0
5 0 0 0 0 0 0 0 13
0 6 0 0 0 0 0 12 0
0 0 7 0 0 0 11 0 0
0 0 0 8 0 10 0 0 0
0 0 0 0 9 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 2 0 16 0 0 0
0 0 3 0 0 0 15 0 0
0 4 0 0 0 0 0 14 0
5 0 0 0 0 0 0 0 13
0 6 0 0 0 0 0 12 0
0 0 7 0 0 0 11 0 0
0 0 0 8 0 10 0 0 0
0 0 0 0 9 0 0 0 0
c program:
#include<stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<2*n-1;i++)
{
for(int j=0;j<2*n-1;j++)
{
if(i==n-j-1)
{
printf("%d ",i+1);
}
else if(i==j-n+1)
{
printf("%d ",4*n-i-3);
}
else if(i==j+n-1)
{
printf("%d ",i+1);
}
else if(n-1==i+j+2-2*n)
{
printf("%d ",4*n-i-3);
}
else
{
printf("0 ");
}
}
printf("\n");
}
}
No comments:
Post a Comment