displaying a pyramid

please help urgent the thing is it should be displayed this way please help with a little description please.........

enter number of linnes:7




7
7 6 7
7 6 5 6 7
7 6 5 4 5 6 7
7 6 5 4 3 4 5 6 7
7 6 5 4 3 2 3 4 5 6 7
7 6 5 4 3 2 1 2 3 4 5 6 7







#include <iostream.h>
#include <conio.h>
int main()
{
int i, k, n;
wronginput:
cout << "\nEnter The Number of lines {1..10}\n";
cin>> n;
if(n < 1 || n >10)
{
cout<<"\nPlease Enter The Number of lines B/W {1..10}\n";
goto wronginput;
}
for (i=1;i<=n;i++)
{
for(k = n-i; k>0;k--)
cout<<(" ");
for(k=1;k<2*i;k++)
cout<<i;
cout<<endl;
}
cout<<endl<<"Press any key to quit";
system("PAUSE");
return 0;
}








Last edited on
A few problems with your code:
1) The correct header is #include <iostream>
2) You need using namespace std;
3) goto should be avoided. Use a do {} while (); loop.
4) You need an additional loop for the right side of the triangle.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

int main()
{	int i, k, n;
	bool goodinput = false;

	do
	{	cout << "Enter The Number of lines {1..10}";
		cin>> n;
		if (n >= 1 && n <= 10) 
			goodinput = true;
	}
	while (! goodinput);
	for (i=n;i>0;i--)
	{	for(k=i; k>=0; k--)
			cout << " ";
		for(k=n;k>=i;k--)
			cout<< k;
		for(k=i+1;k<=n;k++)
			cout<< k;
		cout<<endl;
	}
	system("PAUSE");
	return 0;
}


1
2
3
4
5
6
7
8
Enter The Number of lines {1..10}7
        7
       767
      76567
     7654567
    765434567
   76543234567
  7654321234567


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
Last edited on
Topic archived. No new replies allowed.