By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,391 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

Need to make Pyramid of X , Help

P: 2
Hello,

I need to make a pyramid 20 lines long showing Xs like this:
X
XX
XXX
XXXX
and so on .

I have made this code so far:

Expand|Select|Wrap|Line Numbers
  1. #include<conio.h>
  2. #include<iomanip.h>
  3. void main(void)
  4. {
  5. for ( int i=1; i<=20; i++ )
  6. {
  7. for ( int k=1; k<=i; k++ )
  8. {
  9. cout<<"X";
  10. }
  11. cout<<endl;
  12. }
  13. getch();
  14. }
  15.  
The problem is this doesnt show as a pyramid rather this shows as:
X
XX
XXX
XXXX

What to do?

I need to put a third for loop for setw decrement as k increase but how to set it , when i do that i cant understand how to control it.
Thanks
Nov 17 '06 #1
Share this Question
Share on Google+
4 Replies


P: 2
Anyone can help please?
Nov 18 '06 #2

P: 1
hi,

I have made a program somewhat like this.I believe it will do much to give you an idea.It is the method & not the solution.
[code]
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,x,n,t;
char a,b;
do
{
cout<<"Enter no. of different lines to print (max. 5 lines): ";
cin>>t;
if(t>0&&t<6)
{
for(i=0;i<t+1;i++)
{
cout<<"\n\n";
for(x=0;x<=4-i;x++)
cout<<"\t";
for(n=65;n<=64+i;n++)
{
a=n;
cout<<a<<"\t";
}
for(n=n-2;n>64;n--)
{
a=n;
cout<<a<<"\t";
}
}
for(i=i-2;i>0;i--)
{
cout<<"\n\n";
for(x=0;x<=4-i;x++)
cout<<"\t";
for(n=65;n<=64+i;n++)
{
a=n;
cout<<a<<"\t";
}
for(n=n-2;n>64;n--)
{
a=n;
cout<<a<<"\t";
}
}
}
else
cout<<"\nINVALID LINES";
cout<<"\nDo you want to print more : ";
cin>>b;
}while(b=='y'||b=='Y');
getch();
}

[\CODE]
Reply if it doesn`t work
[\QUOTE]
Nov 23 '06 #3

sivadhas2006
100+
P: 142
Anyone can help please?
Hi,

Do you want the pyramid like this?

Expand|Select|Wrap|Line Numbers
  1.  
  2.    x
  3.   xxx
  4.  xxxxx
  5. xxxxxxx
  6.  
  7.  
Regards,
M.Sivadhas.
Nov 23 '06 #4

sivadhas2006
100+
P: 142
Hello,

I need to make a pyramid 20 lines long showing Xs like this:
X
XX
XXX
XXXX
and so on .

I have made this code so far:

Expand|Select|Wrap|Line Numbers
  1. #include<conio.h>
  2. #include<iomanip.h>
  3. void main(void)
  4. {
  5. for ( int i=1; i<=20; i++ )
  6. {
  7. for ( int k=1; k<=i; k++ )
  8. {
  9. cout<<"X";
  10. }
  11. cout<<endl;
  12. }
  13. getch();
  14. }
  15.  
The problem is this doesnt show as a pyramid rather this shows as:
X
XX
XXX
XXXX

What to do?

I need to put a third for loop for setw decrement as k increase but how to set it , when i do that i cant understand how to control it.
Thanks
Hi,

Your code is working well.
Here is a different way to build the pyramid.

Expand|Select|Wrap|Line Numbers
  1. #include<conio.h>
  2. #include<iomanip.h>
  3.  
  4. void main(void)
  5. {
  6.    int
  7.       i = 0,      
  8.       k = 0,
  9.       nMaxX = 0,
  10.       nSpace = 0,
  11.       nLines = 0;
  12.  
  13.    cout << "Enter the number of lines : " ;
  14.    cin >> nLines;   
  15.  
  16.    nSpace = nLines + (nLines / 2) + nLines % 2;
  17.    nMaxX = -1;
  18.    for (i = 1; i <= nLines; i++)
  19.    {   
  20.       for (k = 1; k <= nSpace; k++ )
  21.       {
  22.          cout << " ";
  23.       }
  24.  
  25.       nSpace--;
  26.       nMaxX = nMaxX + 2;
  27.       for (k = 1; k <= nMaxX; k++)
  28.       {
  29.          cout<<"X";
  30.       }
  31.       cout<<endl;
  32.    }
  33.    getch();
  34. }
  35.  
  36.  
Regards,
M.Sivadhas.
Nov 23 '06 #5

Post your reply

Sign in to post your reply or Sign up for a free account.