Learn about C program to print hollow diamond of numbers 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 hollow diamond of numbers
To print the pattern as below run the below C code with required number of rows.
1 2 2 3 3 4 4 5 5 4 4 3 3 2 2 1
Program:
#include<stdio.h>
int main()
{
int n, a=1;
printf("Enter number of rows: ");
scanf("%d", &n);
for(int i=1; i<=n; i++)
{
for(int j=i; j<=n; j++)
{
printf(" ");
}
for(int k=1; k<=2*i-1; k++)
{
if(k==1 || k==2*i-1) printf("%d",a);
else printf(" ");
}
a++;
printf("\n");
}
a=n-1;
for(int i=n-1; i>=1; i--)
{
for(int j=n; j>=i; j--)
{
printf(" ");
}
for(int k=1; k<=2*i-1; k++)
{
if(k==1 || k==2*i-1) printf("%d",a);
else printf(" ");
}
a--;
printf("\n");
}
return 0;
}
Output:
Enter number of rows: 5 1 2 2 3 3 4 4 5 5 4 4 3 3 2 2 1
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 hollow diamond of stars
C program to print full diamond of numbers, starting with zero and ending with zero