Tuesday, January 4, 2011

Printing Diamond Pattern in C


Coding:

#include<stdio.h>
#include <conio.h>
void main()
{
int i,j,n;
char a='*';
clrscr();
printf("Enter the Number: ");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(j=1;j<=i;j++)
printf("%-2c",a);
printf("\n");
}

n=n-1;

for(i=n;i>=1;i--)
{
for(j=1;j<=n-i;j++)
printf(" ");
for(j=1;j<=i;j++)
printf("%2c",a);
printf("\n");
}
getch();
}

Output:
  

2 comments:

deep said...

can you do dis?
1
1 2 1
1 2 3 2 1
1 2 1
1
f you can plz mail me
but the number of row should be 5
for this prgram
pradipta.das115@gmail.com

Karpov said...

yeah definitely