Sunday, 7 April 2013

PATTERN MACHING IN STRINGS


#include<stdio.h>
#include<conio.h>
void main()
{
 int i,k,l=30,j,sum=0,tsum=0;
 clrscr();
 printf("\n Christmas Gift");
 printf("\n\tDay\t\tGifts\t\t  No of gifts per day");
 for(i=1;i<=12;i++)
 {
  printf("\n\t%d\t\t",i);
  sum=1;
  j=1;
  printf("%d",j);
  for(j=2;j<=i;j++)
  {
   printf("+%d",j);
   sum=sum+j;
  }
  for(k=l;k>1;k--)
    printf(" ");
  if(j<10)
   l=l-2;
  else
   l=l-3;
  printf("%d",sum);
  tsum=tsum+sum;
 }
 printf("\nTotal gifts at the 12th day: %d",tsum);
 getch();
}

  output:

 Christmas Gift
        Day             Gifts             No of gifts per day
        1               1                             1
        2               1+2                          3
        3               1+2+3                                                 6
 4               1+2+3+4                                       10
 5               1+2+3+4+5                                   15
 6               1+2+3+4+5+6                               21
7               1+2+3+4+5+6+7                                        28
 8               1+2+3+4+5+6+7+8                                  36
 9               1+2+3+4+5+6+7+8+9                  45
10              1+2+3+4+5+6+7+8+9+10          55
 11              1+2+3+4+5+6+7+8+9+10+11      66
 12              1+2+3+4+5+6+7+8+9+10+11+12    78
Total gifts at the 12th day: 364

No comments:

Post a Comment