Hello;

i wrote this code for a star triangle , but it told me it contains some errors , Do any 1 can help me plz ?

it should be like this :

*******
 *****
  ***
   *
include <iostream> 
#include<cmath>
using namespace std;
int main()
{   int n=4;
 for ( int i=n;i>0;i--)
 {for (int space=i; space <n; space++)
	{cout<<" ";
	for (int j=1;j<=(2i-1); j++)
     cout<<"*";}
 cout<<endl;
	return 0;
}

Recommended Answers

All 6 Replies

#include <iostream> 
#include<cmath>
using namespace std;
int main()
{   
int n=4;
 for ( int i=n;i>0;i--)
 {
    for (int space=i; space <n; space++)
    {
        cout<<" ";
        for (int j=1;j<=(2*i-1); j++)
         cout<<"*";
 }
 cout<<endl;
}
    return 0;

}

well i removed the errors but it outputs nothing.. do check ur logic again

#include <iostream> 
#include<cmath>
using namespace std;
int main()
{ 
	
	for(int i=0; (i < 7); i++)
	{
		cout << "*";
	}
	
	cout << "\n ";
	
	for(int i=0; (i < 5); i++)
	{
		cout << "*";
	}
	
	cout << "\n  ";
	for(int i=0; (i < 3); i++)
	{
		cout << "*";
	}
	
	cout << "\n   ";
	cout << "*";
return EXIT_SUCCESS;
}
#include <iostream> 
#include<cmath>
using namespace std;
int main()
{   
int n=4;
 for ( int i=n;i>0;i--)
 {
    for (int space=i; space <n; space++)
    {
        cout<<" ";
        for (int j=1;j<=(2*i-1); j++)
         cout<<"*";
 }
 cout<<endl;
}
    return 0;

}

well i removed the errors but it outputs nothing.. do check ur logic again

yaa, it dose not print any thing !
i do not where is the wrong logical statement their .

#include <iostream> 
#include<cmath>
using namespace std;
int main()
{ 
	
	for(int i=0; (i < 7); i++)
	{
		cout << "*";
	}
	
	cout << "\n ";
	
	for(int i=0; (i < 5); i++)
	{
		cout << "*";
	}
	
	cout << "\n  ";
	for(int i=0; (i < 3); i++)
	{
		cout << "*";
	}
	
	cout << "\n   ";
	cout << "*";
return EXIT_SUCCESS;
}

thanks , but it should be written using the same logic or style that i had post it !

any help !

If you go back to your original design,

#include <iostream> 
#include<cmath>
using namespace std;
int main()
{   
    int n=4;
    for ( int i=n;i>0;i--)
    {
        for (int space=i; space <n; space++)
        {
            cout<<" ";
            for (int j=1;j<=(2*i-1); j++)
                cout<<"*";
        }
        cout<<endl;
    }
    return 0;
}

and modify it, it is a viable approach (sort of). First off, you want to move the for() loop that writes the asterisks out of the spacing loop. Second, try making i = 8 to start, and instead of decrementing by one, decrement by 2.

for (int i = 2 * n; i > 0; i -= 2)

You'll need to adjust the spacing loop accordingly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.