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:
# # # # # # # # ## # # # # # #
# # # # #
# # #
#
# # #
# # # # #
# # # # # # #
# # # # # # # # #
your code is amazing!
ReplyDeletecan you explain clrscr();? thank you :D
Thank you!!! ....
Deleteclrscr() 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
Thank you :) :)
ReplyDeletehey what does s mean
ReplyDelete