Thursday, September 8, 2011

Pattern with Nested Loop in C




1
2 2
3 3 3
4 4 4 4
. . . . . .

//Pattern

#include <stdio.h>
#include <conio.h>

void main()
      {
           int i,j,n,k;
           clrscr();
           printf("\nEnter Limit : ");
           scanf("%d",&n);
           for(i=1;i<=n;i++)
              {
                   for(k=1;k<=n-i;k++)       //Spacing Loop
                        {
                            printf(" ");
                        }
                   for(j=1;j<=i;j++)
                        {
                            printf("%d ",i);
                        }
                   printf("\n");
              }
           getch();
      }

*
***
*****
*******


//Pattern

#include <stdio.h>
#include <conio.h>

void main()
      {
           int i,j,n,k;
           clrscr();
           printf("\nEnter Limit : ");
           scanf("%d",&n);
           for(i=1;i<=n;i++)
              {
                   for(k=1;k<=n-i;k++)
                        {
                            printf(" ");
                        }
                   for(j=1;j<=(i*2)-1;j++)
                        {
                            printf("*");
                        }
                   printf("\n");
              }
           getch();
      }

5
54
543
5432
54321


//Pattern

#include <stdio.h>
#include <conio.h>

void main()
      {
           int i,j,n,k;
           clrscr();
           printf("\nEnter Limit : ");
           scanf("%d",&n);
           for(i=n;i>=1;i--)
              {
                   for(k=1;k<=i;k++)
                        {
                            printf(" ");
                        }
                   for(j=n;j>=i;j--)
                        {
                            printf("%d ",j);
                        }
                   printf("\n");
              }
           getch();
      }

  1
  2  5
  6  9 14
 15 18 23 30
 31 34 39 46 55


//Pattern

#include <stdio.h>
#include <conio.h>

void main()
   {
      int n,i,a,b=0,c,j;
      a=0;
      printf("\nEnter Limit : ");
      scanf("%d",&n);
      clrscr();
      for(i=1;i<=n;i++)
           {
              a=b+1;
              c=3;
              for(j=1;j<=i;j++)
                   {
                        printf("%3d",a);
                        b=a;
                        a=a+c;
                        c=c+2;
                   }
                        printf("\n");
           }
    getch();

   }

1
2 3
3 4 5
4 5 6 7
5 6 7 8 9


//Pattern
  void main()
      {
           int i,j,k,m=1,t=0,n;
           clrscr();
           printf("\nEnter Size : ");
           scanf("%d",&n);
           for(i=1;i<=n;i++)
              {
                   for(k=1;k<=(3*n)-i;k++)
                        {
                            printf(" ");
                        }
                   for(j=1;j<=i;j++)
                        {
                            if(j<10)
                                printf("%2d",m);
                          else
                                printf("%3d",m);
                            m++;
                        }
                   m=m+t;
                   t--;
                   printf("\n");
              }
           getch();
      }

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1


//Pattern

#include <stdio.h>
#include <conio.h>

  void main()
      {
           int i,j,n,k;
           clrscr();
           printf("\nEnter Limit : ");
           scanf("%d",&n);
           for(i=1;i<=n;i++)
              {
                   for(k=1;k<=n-i;k++)
                        {
                            printf(" ");
                        }
                   for(j=1;j<=i;j++)
                        {
                            printf("%d ",j);
                        }
                   printf("\n");
              }
           for(i=n-1;i>=1;i--)
              {
                   for(k=1;k<=n-i;k++)
                        {
                            printf(" ");
                        }
                   for(j=1;j<=i;j++)
                        {
                            printf("%d ",j);
                        }
                   printf("\n");
              }
           getch();
      }

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

//Pattern

#include <stdio.h>
#include <conio.h>

void main()
      {
           int i,j,n,k;
           clrscr();
           printf("\nEnter Limit : ");
           scanf("%d",&n);
           for(i=1;i<=n;i++)
              {
                   for(k=1;k<=n-i;k++)
                        {
                            printf(" ");
                        }
                   for(j=1;j<=i;j++)
                        {
                            printf("* ");
                        }
                   printf("\n");
              }
           for(i=n-1;i>=1;i--)
              {
                   for(k=1;k<=n-i;k++)
                        {
                            printf(" ");
                        }
                   for(j=1;j<=i;j++)
                        {
                            printf("* ");
                        }
                   printf("\n");
              }
           getch();
      }

1
121
12321
1234321
123454321
1234321
12321
121
1

//Pattern

#include <stdio.h>
#include <conio.h>

void main()
      {
           int i,j,n,k;
           clrscr();
           printf("\nEnter Limit : ");
           scanf("%d",&n);
           for(i=1;i<=n;i++)
              {
                   for(k=1;k<=n-i;k++)
                        {
                            printf(" ");
                        }
                   for(j=1;j<=i;j++)
                        {
                            printf("%d",j);
                        }
                   for(k=i-1;k>=1;k--)
                        {
                            printf("%d",k);
                        }
                   printf("\n");
              }
           for(i=n-1;i>=1;i--)
              {
                   for(k=1;k<=n-i;k++)
                        {
                            printf(" ");
                        }
                   for(j=1;j<=i;j++)
                        {
                            printf("%d",j);
                        }
                   for(k=i-1;k>=1;k--)
                        {
                            printf("%d",k);
                        }
                   printf("\n");
              }
           getch();
      }

123454321
1234 4321
123     321
12        21
1           1

//Pattern

#include <stdio.h>
#include <conio.h>

void main()
      {
           int i,j,n,k,t,m=-1;
           clrscr();
           printf("\nEnter Limit : ");
           scanf("%d",&n);
           for(i=5;i>=1;i--)
              {
                   for(j=1;j<=i;j++)
                        {
                            printf("%d",j);
                        }
                   for(k=1;k<=m;k++)
                        {
                            printf(" ");
                        }
                   if(i==n)
                        {
                            t=i-1;
                        }
                   else
                        {
                            t=i;
                        }
                   for(k=t;k>=1;k--)
                        {
                            printf("%d",k);
                        }
                   printf("\n");
                   m+=2;
              }
           getch();
      }

Space adjustment was little tough for this one.

Enter Size : 5
7
                           1                   1
                           1 2              2 1
                           1 2 3        3 2 1
                           1 2 3 4   4 3 2 1
                           1 2 3 4 5 4 3 2 1
                           1 2 3 4    4 3 2 1
                           1 2 3         3 2 1
                           1 2               2 1
                           1                     1

//Pattern

#include <stdio.h>
#include <conio.h>

 void main()
   {
  int i,j,k,m,n,t;
  clrscr();
  printf("\nEnter Size : ");
  scanf("%d",&n);
  m=n;
// ******************  Space calculation *********************
  for(i=4;i<=n;i++)
      {
           m++;
      }
// ******************  First Half *********************
  for(i=1;i<=n;i++)
      {
           for(k=0;k<=25;k++)
              {
                   printf(" ");
              }
           for(j=1;j<=i;j++)
              {
                   printf("%2d",j);
              }
        for(k=1;k<=m*2;k++)
           {
              printf(" ");
           }

           if(i==n)
           t=i-1;
           else
           t=i;
           for(j=t;j>=1;j--)
              {
                   printf("%2d",j);
              }

           m-=2;
           printf("\n");
    }
// ******************  Bottom Half *********************
  m=1;
  for(i=n-1;i>=1;i--)
      {
           for(k=0;k<=25;k++)
              {
                   printf(" ");
              }
           for(j=1;j<=i;j++)
              {
                   printf("%2d",j);
              }
        for(k=1;k<=m*2;k++)
           {
              printf(" ");
           }

           for(j=i;j>=1;j--)
              {
                   printf("%2d",j);
              }

           m+=2;
           printf("\n");
    }

      getch();
  }



No comments: