print a asterisk right angled triangle

input: a base number 4
output: where # represent white space
base = 4

######*#      
####*#*#
##*###*#  
*#*#*#*#

input : base = 5
output:

########*#      
######*#*#
####*###*#
##*#####*#  
*#*#*#*#*#



this is the code i have so far it works for 3 & 4 but no >4. can someone please point out what i might be doing wrong
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
27
28
29
30
31
#include <iostream>
using namespace std;

int main()
{
    int base;
    int e=1;
    int f=0;
    int z=base-1+base;

    cin>> base;

    for(int a = 1; a <= base; a++ )
    {
        for(int c = base-a; c>0; c--)
            cout<<"   ";
        for(int b = 1; b <= a; b++)
            if(base>3 && b!=z && a!=1 && a!=base && b==2+f && a==2+e)
        {
            cout << "   ";
            ++f;
            ++e;
        }
        else
        {
            cout << " * ";
        }
            cout << endl;
    }
return 0;
}

Last edited on
Please use code formatting, edit your post and add [code] and [/code] around your program.
And you can use [output][/output] for your example output instead of trying to align it with #.

if(base>3 && b!=z && a!=1 && a!=base && b==2+f && a==2+e)
There's no reason for this convoluted combination of conditionals.

Notice that whenever you print a '*', it is always followed by a '#' (space).
- First outer loop: print (2*n - 2) spaces. Then print 1 (* + space).
- Second outer loop: print (2*n - 4) spaces. Then print 2 (* + space).
- Third outer loop: print (2*n - 6) spaces. Then print 3 (* + space).
- (Do the outer loop n ('base') times)
- See the pattern?
Last edited on
this is my revised code taking into consideration your comments
i know im missing another something, can you point me in the right direction

the output for an input of 3 is correct, but >3
this is what i get for input:4
which is wrong

######*#
####*#*#
*#*#*#*#



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
27
28
29
int main()
{
    int a;
    int f;
    int s;
    int t;
    int n;
    cin>>n;

    for(f=2*n-2;f>0;f--)
    {
            cout<<"#";
    }
    cout<<"*#"<<endl;

    for(s=2*n-4;s>0;s--)
    {
        cout<<"#";
    }
    for(int i =2;i>0;i--)
    {
        cout<<"*#";
    }
    cout<<endl;
    for(t=n;t>0;t--)
    {
        cout<<"*#";
    }
}
Your first example, in my opinion, was closer. You need a nested for loop (for loop within a for loop).
- Each outer for loop handles printing each row
- Each inner for loop handles printing each character within a row
- Furthermore, the inner for loop can actually be two for loops; first, you print the appropriate amount of spaces. Then, you print the appropriate amount of "*#".

See this as an example you can adapt from:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
    int base = 5;

    for (int row = 0; row < base; row++)
    {
        // each row, print spaces, then *
        for (int column = 0; column < base - row - 1; column++)
        {
            cout << "#";
        }
        
        for (int column = base - row - 1; column < base; column++)
        {
            cout << "*";   
        }
        
        cout << '\n';
    }
}

####*
###**
##***
#****
*****

Can you see how to adapt the code to print the pairs of characters you want?
Last edited on

i figured it out, thanks for the help!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
   int base;
   cout << "Enter base: ";   cin >> base;
   for ( int i = 1; i <= base; i++ )
   {
      for ( int j = 1; j <= base; j++ ) cout << " *"[i==base||j==base||i+j==base+1] << ' ';
      cout << '\n';
   }
}


Enter base: 5
        * 
      * * 
    *   * 
  *     * 
* * * * * 
Last edited on
Topic archived. No new replies allowed.