Wednesday, 30 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:

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

4 comments:

  1. your code is amazing!
    can you explain clrscr();? thank you :D

    ReplyDelete
    Replies
    1. Thank you!!! ....
      clrscr() is a function which clears the console output screen....
      it will be available in conio.h header file....
      http://en.wikipedia.org/wiki/Conio.h

      Delete
  2. Thank you :) :)

    ReplyDelete
  3. hey what does s mean

    ReplyDelete