C/C++ How to Create a Pyramid in C++?

  • Thread starter Thread starter jacy
  • Start date Start date
  • Tags Tags
    C++ Pyramid
AI Thread Summary
The discussion centers around creating an equilateral triangle using C++ code, specifically with a height of 8. The original code provided by the user did not produce the desired output, prompting requests for clarification on the intended appearance of the triangle. Key points include the need for proper spacing to achieve the triangle shape and suggestions to use simpler decrement syntax for clarity. Several users provided modified code snippets that included necessary adjustments, such as including the cmath library for mathematical functions and correcting the main function declaration. The conversation emphasizes the importance of formatting and structure in programming to achieve the correct visual output for geometric shapes.
jacy
Messages
76
Reaction score
0
Hello,
I am trying to print an equilateral triangle, made up of * (stars), of height 8. Can someone please give me some hint. I am not getting the desired output. Thank you. Here is my code.

Code:
#include<iostream.h>
#include<iomanip.h>
void main()
{
  int n=8;


	 for (int row=1; row<=8; row++)
	 {
		  for (int col=0; col<row; col++)
		  {

				cout<<setw(n=n-1)<< '*';

		  }
		  cout << endl;  // end the line.
	 }

}
 
Last edited:
Technology news on Phys.org
ew c++ io commands, can't remmeber what the setw command does so it may be best for you to post the errored output you got
...and just printing stars you won't get a real equilateral triangle so I'm guessing your looking for 8 stars each edge.

also are you filling the triangle?

and you can use n-- rather then n=n-1...doesn'treallly matter but n-- looks nicer.

my best guess though is your forgetting to print spaces.
 
jacy said:
Hello,
I am trying to print an equilateral triangle, made up of * (stars), of height 8.

Please show us exactly what this triangle is supposed to look like, by literally typing it out inside a (code)(/code) block. (square brackets instead of parentheses, of course).
 
Code:
#include<iostream.h>

main()
{
  int n=8;

	 cout<<"45,90,45 degree triangle"<<endl;
	 for (int y=1; y<=8; y++)
	 {
		for(int space=8-y; space>0; space--)
		{
			cout<<" ";
 		}	
		for (int x=0; x<y; x++)
		{
			cout<<"**";
		}		 
		cout << endl;  // end the line.
	}

	cout<<endl<<"circle"<<endl;
	for (double y=-10; y<=10; y++)
	{
		for (double x=-10; x<=10; x++)
		{
			if(x*x+y*y<=100)
				cout<<"*";
			else
				cout<<" ";
		}		
		cout << endl;  // end the line.
	}

	cout<<endl<<"Equilateral Triangle"<<endl;
	for (double y=7*sqrt(3); y>=0; y--)
	{
		for (double x=-7; x<=7; x++)
		{
		if(x==0)
			cout<<"*";
		else if(x<0&&y/(x+7)<=sqrt(3))
			cout<<"*";
		else if(x>0&&y/(7-x)<=sqrt(3))
			cout<<"*";
		else
			cout<<" ";
		}		
		cout << endl;  // end the line.
	}
	system("PAUSE");
}
 

Attachments

  • 45triangle-circle-60triangle.gif
    45triangle-circle-60triangle.gif
    8.4 KB · Views: 805
Last edited:
Thanks for the help
 
Your Welcome. :smile:
 
Please don't do people's homework for them.
 
something wrong

when i tried to run the programm it had some problems.this is how i change it but it works

#include<iostream.h>
#include <cmath>//the first change
int main()
{
int n=8;


cout<<"45,90,45 degree triangle"<<endl;
for (int y=1; y<=8; y++)
{
for(int space=8-y; space>0; space--)
{
cout<<" ";
}
for (int x=0; x<y; x++)
{
cout<<"**";
}
cout << endl; // end the line.
}

cout<<endl<<"circle"<<endl;
for ( y=-10; y<=10; y++)//second change
{
for (double x=-10; x<=10; x++)
{
if(x*x+y*y<=100)
cout<<"*";
else
cout<<" ";
}
cout << endl; // end the line.
}

cout<<endl<<"Equilateral Triangle"<<endl;
for (y=7*sqrt(3); y>=0; y--)//third change
{
for (double x=-7; x<=7; x++)
{
if(x==0)
cout<<"*";
else if(x<0&&y/(x+7)<=sqrt(3))
cout<<"*";
else if(x>0&&y/(7-x)<=sqrt(3))
cout<<"*";
else
cout<<" ";
}
cout << endl; // end the line.
}

return 0;//fourth change

}
 
Back
Top