Diamond Pattern C program

Q. Write a C program to print the following diamond pattern as:

4
434
43234
4321234
43234
434
4

Ans.

/*c program for diamond pattern*/
#include<stdio.h>
int main()
{
 int num,r,c,z,n,nm,no;
 printf("Enter max. number : ");
 scanf("%d", &num);
 nm = num+1;
 for(r=1; r<=num; r++,nm--)
 {
  for(c=1,n=num; c<=r; c++,n--)
     printf("%d",n);
  for(z=nm,c=1; c<r; c++,z++)
     printf("%d",z);
  printf("\n");
 }

 no = num-1;
 for(r=1; r<=num; r++,no++)
 {
  for(c=num; c>r; c--)
     printf("%d",c);
  for(z=no,c=num-2; c>=r; c--,z++)
     printf("%d",z);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of diamond pattern C program
Figure: Screen-shot for diamond pattern C program


Comments