Sunday, 6 April 2014

Pattern Printing

Program to print particular pattern
#include<stdio.h>
main()
{
int i,j,n;
char ch='a';
printf("eneter value for n\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
//printf("\n%d\n",i);
 for(j=1;j<=i;j++)
 {
 printf("%5c",ch++);
 }
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex27.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
eneter value for n
5
    a
    b    c
    d    e    f
    g    h    i    j
    k    l    m    n    o
Program to print RHOMBOUS Pattern
#include<stdio.h>
main()
{
int i,j,n,k;
printf("enter value for n\n");
scanf("%d",&n);
for(i=-n;i<=n;i++)
{
        for(j=1;j<=n;j++)
        {
        k=i;
        if(k<0)
        k=-1*k;
        if(j<=k)
        printf(" ");
        else
        printf("* ");
        }
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex28.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter value for n
5
     
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    *
Program pattern for REVERSE RHOMBOUS
#include<stdio.h>
main()
{
int i,j,k,n;
printf("enter value for n\n");
scanf("%d",&n);
for(i=-n;i<=n;i++)
{
for(j=n;j>=0;j--)
{
        k=i;
        if(k<0)
        k=-k;
if(j>=k)
printf(" ");
else
printf("* ");
}
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex29.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter value for n
5
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 
      
     * 
    * * 
   * * * 
  * * * * 
 * * * * *
Program to print following pattern
#include<stdio.h>
main()
{
int i,j,n;
printf("enter value for n\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=-n;j>=n+1;j++)
printf(" "); 
for(j=n;j>=i;j--)
printf("* "); 
printf("\n");
}
for(i=1;i<=n+1;i++) 
{
for(j=1;j<=n;j++)
printf(" ");
for(j=1;j<=i;j++)
printf("* ");
printf("\n");
}
} 
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex30.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter value for n
5
* * * * * 
* * * * 
* * * 
* * 
* 
     * 
     * * 
     * * * 
     * * * * 
     * * * * * 
     * * * * * *
program to print triangle pattern
#include<stdio.h>
main()
{
int i,j,n;
printf("enter value for n\n");
scanf("%d",&n);
/*i=1;
while(i<=5)
{
 j=1;
 while(j<=10)
 {
 printf("%d\t",i*j);
 j++;
 }
i++;
printf("\n");
}*/

/*for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}*/

/*
for(i=1;i<=n;i++)
{
for(j=1;j<=n-1;j++)
{
printf(" ");
for(j=1;j<=i;j++)
{
printf(" * ");
}
}
printf("\n");
}*/

for(i=n;i>0;i--)
{
for(j=1;j<=n;j++)
{
if(j<i)
printf(" ");
else
printf("* ");
}
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc ex46.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter value for n
5
    * 
   * * 
  * * * 
 * * * * 
* * * * *
Program to print pattern
#include<stdio.h>
main()
{
int i,j,n;
printf("enter value for n\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-1;j++)
printf(" ");
for(j=1;j<=i;j++)
printf(" * ");
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pyramid.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter value for n
5
     * 
     *  * 
     *  *  * 
     *  *  *  * 
     *  *  *  *  *
Program to print pattern
#include<stdio.h>
main()
{
int i,j,k,n;
printf("enter value for n\n");
scanf("%d",&n);
for(i=-n;i<=n;i++)
{
k=i;
if(k<0)
k=-k;
for(j=0;j<n;j++)
{
if(j<=k)
printf(" ");
else
printf("* ");
}
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pyramid2.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter value for n
5
     
     
    * 
   * * 
  * * * 
 * * * * 
  * * * 
   * * 
    *
Program to print pattern
#include<stdio.h>
main()
{
int i,j,k,n;
char ch='A';
printf("enter value for n\n");
scanf("%d",&n);
for(i=-n;i<=n;i++)
{
k=i;
if(k<0)
k=-k;
for(j=0;j<=n;j++)
{
if(j<=k)
printf(" ");
else
{
printf("%c ",ch);
ch++;
}
}
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pyramid3.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter value for n
5
      
     A 
    B C 
   D E F 
  G H I J 
 K L M N O 
  P Q R S 
   T U V 
    W X 
     Y
Program to print pattern
#include<stdio.h>
main()
{
int i,j,k,n;
printf("enter value for n\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<=2*n-i;j++)
printf(" ");
for(j=0;j<=2*i;j++)
printf("*");
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc pyramid4.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter value for n
5
           *
          ***
         *****
        *******
       *********