Thursday, May 31, 2012

Print the pattern using C programs


Few programs on pattern using C language are posted here.
Pattern number 1:
1
1 2 3
1 2 3 4 5
#include < stdio.h>
void main()
{
 int i,j,x;
 clrscr();
 for(i=0;i<3;i++)
 {
 x=1;
  for(j=0;j< i*2+1;j++)
  {
  printf("%d",x++);
  }
  printf("\n");
  }
  getch();
  }


Pattern number 2:
1234
123
12
1

#include < stdio.h>
void main()
{
 int i,j,x;
 clrscr();
 for(i=0;i<4;i++)
 {
 x=1;
  for(j=0;j<4-i;j++)
  {
  printf("%d",x++);
  }
  printf("\n");
  }
  getch();
  }

Pattern number 3:

Another pattern as follows, here ‘*’ indicates blank space.
****1
***12
**123
*1234
12345

#include < stdio.h>
void main()
{
 int i,j,k,x;
 clrscr();
 for(i=0;i<5;i++)
 {
 x=1;
 for(k=i;k<4;k++)
 printf(" ");
  for(j=0;j<=i;j++)
  {
  printf("%d",x++);
  }
  printf("\n");
  }
  getch();
  }

Pattern number 4:

Next pattern is like:
5
54
543
5432
54321

#include < stdio.h>
void main()
{
 int i,j,x;
 clrscr();
 for(i=0;i<5;i++)
 {
 x=5;
   for(j=0;j<=i;j++)
  {
  printf("%d",x--);
  }
  printf("\n");
  }
  getch();
  }

Pattern number 5:

Pattern printing the numbers 1 2 2 3 3 4 4 5 5 as inverted ‘V’, like:
#include < stdio.h>
void main()
{
 int i,j,k,x=1,y=1;
 clrscr();
 for(i=0;i<5;i++)
 {
   for(k=i;k<5;k++)
   printf(" ");
   for(j=0;j< y;j++)
  {
  if(j==0||j==y-1)
  printf("%d",x);
  else
  printf(" ");
  }
  y=y+2;
  x++;
  printf("\n");
  }
  getch();
  }

2 comments:

Subscribe via email

Enter your email address:

Delivered by FeedBurner