Learn about C program to print full diamond of numbers, starting with zero and ending with zero in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
C program to print full diamond of numbers, starting with zero and ending with zero
0 010 01210 0123210 012343210 01234543210 0123456543210 01234543210 012343210 0123210 01210 010 0
Program:
#include<stdio.h>
int main()
{
int n, a=0;
printf("Enter value of n: ");
scanf("%d",&n);
for(int i=1; i<=n+1; i++)
{
for(int j=i; j<=n; j++)
{
printf(" ");
}
for(int k=1; k<=2*i-1; k++)
{
if(k<i) printf("%d",a++);
else if(k==i) printf("%d",a);
else printf("%d",--a);
}
printf("\n");
}
for(int i=n; i>=1; i--)
{
for(int j=n; j>=i; j--)
{
printf(" ");
}
for(int k=1; k<=2*i-1; k++)
{
if(k<i) printf("%d",a++);
else if(k==i) printf("%d",a);
else printf("%d",--a);
}
printf("\n");
}
return 0;
}
Output:
Enter value of n: 6 0 010 01210 0123210 012343210 01234543210 0123456543210 01234543210 012343210 0123210 01210 010 0
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
C program to print full diamond of numbers
C program to print full diamond of spaces and stars