I can do the pattern totally for * or number only but how to use both together for pattern?
Here is the pattern i want to do.

0
* *
0 1 2
* * * *
0 1 2 3 4
* * * *
* * *
* *
*

#include <iostream>

using namespace std;

int main()

{
    for (int a=1; a<=5; a++)
    {
        for (int b=1; b<=a; b++)
        {
            for (int i=0; i<b; i++)
            {
                 cout<<i<<" ";
            }
            cout<<endl;
        }
        
        for (int b=1; b<=a; b++)
        {
            for (int i=0; i<b; i++)
            {
                 cout<<"* ";
            }
            cout<<endl;
        }
    }
        
    for (int c=1; c<=4; c++)
    {
        for (int d=4; d>=c; d--)
        {
            cout<<"* ";
        }
        
        cout<<endl;
    }
    
    
system ("pause");
return 0;

}

Recommended Answers

All 2 Replies

You have your for loops all messed up so it is outputting sections more than once in a row.

#include <iostream>
using namespace std;

int main()
{
	int num = 0;
	cout << "How big do you want the pyramid? ";
	cin >> num;

	for( int i = 0; i < num; i++ )
	{
		for( int c = 0; c <= i; c++ )
			cout << c << " ";
		cout << endl;

		i++;
		for( int j = 0; i < num && j <= i; j++ )
			cout << "*";
		if( i < num )
			cout << endl;
	}
	
	for( int i = num-1; i >= 0; i-- )
	{
		for( int c = 0; c < i; c++ )
			cout << "*";
		cout << endl;
	}

	return 0;
}

Divide and conquer. In this case, if you break it up into function you will see that
the logic is easy to see.

#include <iostream>
using namespace std;

void printN(const char c, int N){
 while(N--) cout << c;
}
void printSeq(int start, int end){
  while(start != end){ cout << start++; }
}
int main(){
 const int MaxHeight = 5;

 for(int i = 1; i <= MaxHeight; ++i){
    if(i % 2 == 1) printN('*',i);
    else printSeq(0,i);
    cout << endl;
  }
 for(int j = MaxHeight; j; --j){
    if(j % 2 == 0) printN('*',j);
    else printSeq(0,j);
    cout << endl;

 }

  return 0;
}
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.