Saturday, 4 April 2015

Reverse pyramid pattern program in c

#include<stdio.h>
main()
{
int i,j,k=1,m,l;
printf("Enter the height of the pyramid");
scanf("%d",&l);
m=0;
for(i=1;i<=l;i++)
{
    for(j=2*l-1;j>=i;j--)
      {
          if(k==0)
       {
 if((j%2==0) && j<=2*l-m && (j-m)>=i-m)
 printf("%c",'*');
 else
 printf(" ");
 }
           else if(k==1)
 {
 if(j%2!=0 && j<=2*l-m && (j-m)>=i-m)
 printf("%c",'*');
 else
 printf(" ");
 }
         }
printf("\n");
if(k==1)
k=0;
else
k=1;
m=m+1;
}
}

I/O of Program

Enter the height of the pyramid 7                                                                                                                                                
* * * * * * *                                                                                                                                                                   
 * * * * * *                                                                                                                                                                    
  * * * * *                                                                                                                                                                     
   * * * *                                                                                                                                                                      
    * * *                                                                                                                                                                       
     * *                                                                                                                                                                        
      *   

Thursday, 2 April 2015

Efficient C Programming Code To Create Pyramid and Pattern

//only two for loop are used to create pyramid and patterns. Time complexity of the following //program is O(n^2).

 #include<stdio.h>
main()
{
int i,j,k=1,m,l;
printf("Enter the height of the pyramid");
scanf("%d",&l);
m=l;
for(i=1;i<=l;i++)
{
    for(j=1;j<l+i;j++)
      {
          if(k==0)
      {
if((j%2==0) && j>=m && (j-m)<2*i)
printf("%c",'*');
else
printf(" ");
}
           else if(k==1)
{
if(j%2!=0 && j>=m && (j-m)<2*i)
printf("%c",'*');
else
printf(" ");
}
         }
printf("\n");
if(k==1)
k=0;
else
k=1;
m=m-1;
}
}

I/O of a program

Enter the height of the pyramid 7

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