Sunday, September 16, 2012

program to print the diamond


/*program for diamond*/
#include<conio.h>
#include<stdio.h>
void main()
{
int n,i,j,p=1,k,z;
clrscr();
printf("Enter the value of n\n");
scanf("%d",&n);
z=n;
for(i=0;i<z;i++)
{
 for(j=0;j<n;j++)
  {
  printf(" ");
  }
  for(k=1;k<=p;k++)
     {
     printf("*");
     }
     printf("\n");
     p=p+2;

  n--;
}
if(i==z)
{
int m=1;
n=z;
for(i=0;i<z;i++)
{
 for(j=0;j<m+1;j++)
  {
  printf(" ");
  }
  for(k=1;k<=(p-4);k++)
     {
     printf("*");
     }
     printf("\n");
     p=p-2;

  m++;
}
}
getch();
}
This program prints '*' in shape of diamond. First we have to input ,how many '*' the diagonal of the diamond have. The program then produce the required diagonal.If we want to make the diagonal of some other character ,it is so simple ie we need to replace the '*' by the required value.

No comments:

Post a Comment