Advance pattern programs in c

These programs print various patterns of numbers, stars, and spaces. These codes illustrate how to create various patterns using c program. The C programs involve the usage of multiple nesting while loops. 

Example#1

#include <stdio.h>
int main()
{
  int i,n,s,dn;
  printf("Enter a value: ");
  scanf("%d",&n);
  i=1;
  while(i<=n)       // Loop to print rows (outer loop)
  {
    printf("\n");
    s=1;
    while(s<=n-i)    // Loop to print spaces
    {
      printf(" ");
      s=s+1;
    }
    dn=i;
    while(dn>=1)
    {
      printf("%d",dn);   // Loop to print numbers
      dn=dn-1;
    }
    i=i+1;
  }
  return 0;
}

[quote]

Output

Enter a value: 6 
     1                                                                                                                                                       
    21                                                                                                                                                       
   321                                                                                                                                                       
  4321                                                                                                                                                       
 54321                                                                                                                                                       
654321 

[/quote]

Example#2

#include <stdio.h>

int main()
{
  int i,n,in,s;
  printf("Enter a value: ");
  scanf("%d",&n);
  i=n;
  while(i>=1)
  {
    printf("\n");
    s=1;
    while(s<=n-i)
    {
      printf(" ");
      s=s+1;
    }
    in=1;
    while(in<=i)
    {
      printf("%d",in);
      in=in+1;
    }
    i=i-1;
  }
  return 0;
}

[quote]

Output

Enter a value: 6                                                                                                                                        
123456                                                                                                                                                       
 12345                                                                                                                                                       
  1234                                                                                                                                                       
   123                                                                                                                                                       
    12                                                                                                                                                       
     1  

[/quote]

Example#3

#include <stdio.h>
int main()
{
  int i,n,in,dn,s;
  printf("Enter a value: ");
  scanf("%d",&n);
  i=1;
  while(i<=n)
  {
    printf("\n");
    s=1;
    while(s<=n-i)
    {
      printf(" ");
      s=s+1;
    }
    in=1;
    while(in<=i)
    {
      printf("%d",in);
      in=in+1;
    }
    dn=i-1;
    while(dn>=1)
    {
      printf("%d",dn);
      dn=dn-1;
    }
    i=i+1;
  }
  return 0;
}

[quote]

Output:

Enter a value: 6 
     1                                                                                                                                                       
    121                                                                                                                                                      
   12321                                                                                                                                                     
  1234321                                                                                                                                                    
 123454321                                                                                                                                                   
12345654321

[/quote]

Example#4

#include <stdio.h>
int main()
{
  int i,n,in,dn,s;
  printf("Enter a value: ");
  scanf("%d",&n);
  i=n;
  while(i>=1)
  {
    printf("\n");
    s=1;
    while(s<=i-1)
    {
      printf(" ");
      s=s+1;
    }
    dn=n;
    while(dn>=i)
    {
      printf("%d",dn);
      dn=dn-1;
    }
    in=i+1;
    while(in<=n)
    {
      printf("%d",in);
      in=in+1;
    }
    i=i-1;
  }    //end of 1st loop
  i=2;
  while(i<=n)
  {
    printf("\n");
    s=1;
    while(s<=i-1)
    {
      printf(" ");
      s=s+1;
    }
    dn=n;
    while(dn>=i)
    {
      printf("%d",dn);
      dn=dn-1;
    }
    in=i+1;
    while(in<=n)
    {
      printf("%d",in);
      in=in+1;
    }
    i=i+1;
  }
  return 0;
}

[quote]

Output:

Enter a value: 6  
     6                                                                                                                                                       
    656                                                                                                                                                      
   65456                                                                                                                                                     
  6543456                                                                                                                                                    
 654323456                                                                                                                                                   
65432123456                                                                                                                                                  
 654323456                                                                                                                                                   
  6543456                                                                                                                                                    
   65456                                                                                                                                                     
    656                                                                                                                                                      
     6 

[/quote]

Example#5

#include <stdio.h>
int main()
{
  int i,n,in,dn,s;
  printf("Enter a value: ");
  scanf("%d",&n);
  i=1;
  while(i<=n)
  {
    printf("\n");
    in=1;
    while(in<=i)
    {
      printf("%d",in);
      in=in+1;
    }
    s=1;
    while(s<=n-i)
    {
      printf("  ");
      s=s+1;
    }
    dn=i;
    while(dn>=1)
    {
      printf("%d",dn);
      dn=dn-1;
    }

    i=i+1;
  }
  return 0;
}

[quote]

Output:

Enter a value: 6                                                                                                                                             
                                                                                                                                                             
1          1                                                                                                                                                 
12        21                                                                                                                                                 
123      321                                                                                                                                                 
1234    4321                                                                                                                                                 
12345  54321                                                                                                                                                 
123456654321

[/quote]

Leave a Reply

Your email address will not be published. Required fields are marked *