Thursday 15 March 2018

Pattern Programs in C



Write a program to print the above pattern

#include <stdio.h>

int main()
{
    int r,c, n,nr,nc,c1=1,c2=1;
    scanf("%d", &n);
    nr=2*n-1;
    nc=2*n-1;
    for(r=1;r<=nr;r++)
    {
        for(c=1;c<=nc;c++)
        {
            if(c<c1||c>c2)
                printf(" ");
            else
                printf("*");
        }
        if(r<n)
        {
            c1++;
            c2=c2+2;
        }
        else
        {
            c1--;
            c2=c2-2;
        }
        printf("\n");
    }
}

==================================================

http://chitranshugupta.blogspot.com/p/c.html#pt6 

/* Write a Program to print Butterfly Matrix Pattern  
   *       *
   **     **
   ***   ***
   **** ****
   *********
   **** ****
   ***   ***
   **     **
   *       *
*/
 
       int main()
       {
              int n=5;//No. of rows
              int i=1;
              int c1=1;
              int c2=2*n-1;
  
              int j=n+1;
              int b1=n;
              int b2=n;

              for(int row=1;row<=(2*n-1);row++)
              { 
  
                      for(int col=1;col<=(2*n-1);col++)
                      { 
                                 if((row==i && col >c1 && col< c2) || (row==j && col >=b1 && col<=b2))
                                                printf(" ");      
                                 else
                                                printf("*");
                      } 
                      if(row< n)
                      {
                           i++;
                           c1++;
                           c2--;
                      } 
                      else if(row >n)
                      {
                           j++;
                           b1--;
                           b2++;
                      }
                      printf("\n");
              }
             return 0;
       }


Write a C program to print the following number pattern



#include <stdio.h>
main()
{
int n,i,j,k,spaces,prints,c;
scanf("%d",&n);
spaces = n-1;
prints = 1;
for(i=1;i<=2*n-1;i++)
{
for(j=1;j<=spaces;j++)
printf("   ");
c=1;
for(k=1;k<= 2*prints -1; k++)
{
printf("%3d",c);
if(c<n)
c++;
else
c--;
}
if(i<n)
{
spaces--;
prints++;
}
else
{
spaces++;
prints--;
}
printf("\n");
}
}
====================================================================
Write a C program to print the following pattern in C


#include <stdio.h>
main()
{
int n,i,j,k,l;
scanf("%d",&n);
int n1=2*n-1;
int t=n1/2,c=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=t;j++)
printf("*");
for(k=1;k<=c;k++)
printf(" ");
for(;j<=(n1-c);j++)
printf("*");
if(c == 0)
c=c+1;
else
{
c=c+2;
t=t-1;
}
printf("\n");
}
}
===================================================================
Write a C program to print the following pattern




Program:
#include <stdio.h>
int max(int a,int b)
{
    if(a>b)
        return a;
    return b;
}
main()
{
    int n,i,j,l;
    scanf("%d",&n);
    l=2*n+1;
    for(i=0;i<l;i++){
        for(j=0;j<l;j++)
            printf("%d",max(abs(n-i),abs(n-j)));
        printf("\n");
    }
}




Write a C program to print the following pattern

                    1
              1    2
         4   3    9


#include<stdio.h>
main()
{
int n,c=1,flag=1,i,j,k;
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=n-1;j>i;j--)
{
printf("   ");
}
for(k=0;k<=i;k++)
{
if(flag){
printf("%3d",c);flag=0;}
else{
printf("%3d",c*c);flag=1;
c++;
}
}
printf("\n");
}
}


========================================================================

1
1          1
2          1
1          2          1          1
1          1          1          2          2          1

in the above pattern for example n= 1211
1          2          1          1
units and tens place digits are same digit ‘1’ and count =2 ,j =1, n1 =0
so n1= (10*count+same_digit)*j +n1
n1=(10*2+1)*1+0=21
and n becomes 12 i.e, n =12
in 12, units and tens place digit are not same. digit 2 occurs once so count =1 , j=100 , n1=21
n1 = (10*1+2)*100+21 = 1221
now n =1
1 occurs only once so count = 1, j= 10000,n1=1221
n1=(10*1+1)*10000+1221
n1 = 11*10000+1221= 111221
#include <stdio.h>
int main()
{
            int i,n=1,j,n1,temp,c,rows;
            printf("enter number of rows\n");
            scanf("%d",&rows);
            for(i=0;i<rows;i++)
            {
                        n1=0;
                        j=1;
                        printf("%d\n",n);
                        while(n)
                        {
                                    temp=n%10;
                                    n=n/10;
                                    c=1;
                                    while(temp == n%10)   // number of digits same starting from units place
                                    {
                                               c++;
                                               n=n/10;
                                    }
                                    n1=(((10*c)+temp)*j)+n1;   
                                    j=j*100;
                        }
                        n=n1;
            }
            return 0;
}





============================================
to print
*
*   *
*   *    *

in first line, we print one * and
in second line, we print  * * (2 stars) and
in third line, we print * * * (3 stars) and
as i is giving the line number,so j runs i times. 
to print i *'s in ith line

========================================
n=4
* * * *

* * * *

* * * *

* * * *
in first line, we print * * * * (n stars) and in second line, we print   * * * * (n stars) and in third line, we print * * * * (n stars) and so j runs n times. to print n *'s in each line

No comments:

Post a Comment