how do i display an inverted pyramid of asterisks ?...using 'for' loops, 'if' conditions..etc...
as in to display a straight pyramid the code would be:
#include%26lt;iostream.h%26gt;
#include%26lt;conio.h%26gt;
void main()
{clrscr();
int i,j,k,l=1,num;
cout%26lt;%26lt;"Enter the no:"%26lt;%26lt;endl;
cin%26gt;%26gt;num;
for(k=num;k%26gt;=1;k--)
{ for(i=k;i%26gt;=1;i--)
cout%26lt;%26lt;" ";
if(l%26lt;num)
{ for(j=1;j%26lt;=l;j++)
cout%26lt;%26lt;"*"%26lt;%26lt;" ";
}
l++;
cout%26lt;%26lt;endl;
}
getch();
}
Display this design using C++..?
//
// Display a pyramid of asterisks, upright or inverted:
// usage: ./pyramid %26lt;n%26gt;
// if n %26lt; 0, the pyramid is inverted.
//
#include %26lt;iostream%26gt;
#include %26lt;iomanip%26gt;
#include %26lt;cstdio%26gt;
using namespace std;
int main(int argc, char *argv[])
{
int n, startX, endX, increment;
const string star("*");
if ((argc %26lt; 2) || (sscanf(argv[1],"%d",%26amp;n) != 1))
{
cout %26lt;%26lt; "usage : " %26lt;%26lt; argv[0] %26lt;%26lt; " n" %26lt;%26lt; endl;
return -1;
}
if (n %26lt; 0) // inverted
{
n *= -1;
startX = n-1;
endX = increment = -1;
}
else // normal
{
startX = 0;
endX = n;
increment = 1;
}
for (int i = startX; i != endX; i += increment)
{
cout %26lt;%26lt; setw(n-i) %26lt;%26lt; star;
for (int j = 1; j %26lt; (i*2)+1; j++) cout %26lt;%26lt; star;
cout %26lt;%26lt; endl;
}
return 0;
}
Reply:Basically, you need to swap the for loop that prints the spaces with the for loop that prints the *.
#include%26lt;iostream.h%26gt;
#include%26lt;conio.h%26gt;
int main()
{
clrscr();
int i,j,k,l=1,num;
cout%26lt;%26lt;"Enter the no:"%26lt;%26lt;endl;
cin%26gt;%26gt;num;
for(k=num; k%26gt;=1; k--)
{
for(j=1; j%26lt;=l; j++)
{
cout%26lt;%26lt;" ";
}
if( l %26lt;= num)
{
for(i=k; i%26gt;=1; i--)
cout %26lt;%26lt; "*" %26lt;%26lt; " ";
}
l++;
cout%26lt;%26lt;endl;
}
getch();
return 0;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment