Write a program which takes a single integer input "height" and displays a "pyramid"
of this height made up of "*" characters on the screen.
Sample Output:
Pick another height (must be between 1 and 30): the triangle must be equilataural
*
***
*****
*******
*********
***********
*************

Edited by Heba91: n/a

4
Contributors
9
Replies
10
Views
7 Years
Discussion Span
Last Post by bsewell

Use 2 for loops for this.

int height=0;
for(int i=0;i<height;i++)
{
for(int j:0;j<height;j++)
{
}
}

That's a skeleton of what you need to do. It sounds like a homework assignment so fill in the blanks and it should work

Edited by bsewell: n/a

it didn't work i need an equilataural triangle and my problem is to make spaces before the stars
....*******....

Edited by Heba91: n/a

my problem is to make spaces before the stars

eg, your program needs to make a decision on whether to show a space or star.

Edited by bsewell: n/a

........*
......***
....*****
...*******
.*********
***********
*************
this is the shape what i need

Then we can tell you exactly what you did wrong.

Or are you hoping that if you roll around helpless for long enough that someone will spoon-feed you the answer?

It's a choice, but it basically boils down to whether you want to learn or not. At the moment, votes favour the "no" side.

#include <iostream>

using namespace std;

int main()
{
int x,i,n,j;
cout<<"enter the odd number of rows :";
cin>>x;
for(n=x+1;n>=0;n--){
cout<<" ";
for (i=1;i<=x+1;i=i+2){
for(j=i;j<=x+1;j++){
cout<<"*";
}cout<<endl;}}
return 0;
}

(this is what i did but it also didn't work)

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

you went wrong in not using code tags.
You went wrong in writing C++ code when you're supposed to make a Java program.

start quote:

#include <iostream>

using namespace std;

int main()
{
int x,i,n,j;
cout<<"enter the odd number of rows :";
cin>>x;
for(n=x+1;n>=0;n--){
cout<<" ";
for (i=1;i<=x+1;i=i+2){
for(j=i;j<=x+1;j++){
cout<<"*";
}cout<<endl;}}
return 0;
}

(this is what i did but it also didn't work)

That is c++ code, but this is the java forum.

You seem to have omitted something very basic in programming. Have you not heard of "conditional statements"?

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.