April 2014

C++ Hourglass pattern


  C++ program to print the hourglass pattern using '#' symbols. In this program two for loops are used to print the pattern. The first loop prints the upper part to the hourglass (an inverted triangle ).
The second for loop prints the lower part of the hourglass ( triangle part ). By combining both of the triangles an hourglass structure is formed.

PROGRAM CODE:

 #include<iostream.h>  
 #include<conio.h>  
 void main()  
 {  
     int i,j,k=9,s=0,l;  
     clrscr();  
     for(i=0;i<5;i++,k-=2,s+=1)  
     {  
          for(l=s;l>=0;l--)  
          cout<<' ';  
          for(j=k;j>0;j--)  
               cout<<"#";  
          cout<<endl;  
     }  
     for(i=0;i<5;i++,k-=2,s-=1)  
     {  
          for(l=s-1;l>=0;l--)  
          cout<<' ';  
          for(j=k;j<0;j++)  
              cout<<"#";  
          cout<<endl;  
    }  
    getch();  
 }  

OUTPUT:

# # # # # # # # #
   # # # # # # #
      # # # # #
        # # #
           #
        # # #
     # # # # #
   # # # # # # #
# # # # # # # # #

C++ Triangle pattern


PROGRAM CODE :

 #include<iostream.h>  
 #include<conio.h>  
 void main()  
 {  
          char a[]="program";  
          int i,j;  
          clrscr();  
          for(i=0;i<7;i++)  
          {  
                   for(j=0;j<=i;j++)  
                   {  
                           cout<<a[j]<<'\t';  
                   }  
                   cout<<endl;  
           }  
           getch();  
 }  

OUTPUT :

   p
   p    r 
   p    r    o 
   p    r    o    g 
   p    r    o    g    r 
   p    r    o    g    r    a
   p    r    o    g    r    a    m