Right angle triangles.

A program that displays a right angle triangle pattern.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
	int n;

	cout<<"Enter a number: ";

	cin>>n;

	for (int a = 1; a <= n; a++)
	{
		for (int b = 1; b <= a; b++)
		{
			cout<<b<<" ";
		}

		cout<<endl;
	}

	return 0;
}


The above code gives the following output.


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
:
:
1 2 3 4 5....n


But,
What to do to show these following patterns?

Pattern 1:

1 2 3 4 5....n
:
:
1 2 3 4 5 
1 2 3 4
1 2 3
1 2
1



and,
Pattern 2

             1
           1 2
         1 2 3
       1 2 3 4
     1 2 3 4 5
             :
             :
1 2 3 4 5....n


and,
Pattern 3


1 2 3 4 5....n
             :
             :
     1 2 3 4 5 
       1 2 3 4
         1 2 3
           1 2
             1


in separate programs???

Last edited on
Pattern 1,
instead of looping from 1 to n, loop from n to 1

Pattern 2,
you'll need another loop. One will print ' ' a-1 times and the other will do what your working example does.

Pattern 3,
combine 1 and 2..
can u please post the codes. u see i am just a beginner.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<iostream>
#include<conio.h>

using namespace std;

//---------------------------------------------------------------------------


void drawTriangle(int Value)
{
	for(int y = 1; y <= Value; y++)
	{
        	for(int x = 1; x <= y; x++)
		{
			cout << x <<" ";
		}
		cout << endl;
	}
}

void drawUpsideDownTriangle(int Value)
{
	for(int y = Value; y > 0; y--)
	{
        	for(int x = 1; x <= y; x++)
		{
			cout << x <<" ";
		}
		cout << endl;
	}
}


void drawLeftAdjTriangle(ValueN)
{
    for (int y = 1; y <= ValueN; y++)
	{
                for(int s = ValueN-y; s>0; s--)
                        cout << "  "; //two spaces

        	for(int x = 1; x <= y; x++)
		{
			cout << x <<" ";
		}
		cout << endl;
	}
}

void drawLeftAdjUpsideDownTriangle(ValueN)
{
	for(int y = ValueN; y > 0; y--)
	{
                for(int s = 0; s<ValueN-y; s++)
                        cout << "  "; //two spaces

        	for(int x = 1; x <= y; x++)
		{
			cout << x <<" ";
		}
		cout << endl;
	}
}


void main()
{
        int ValueN = 0;

        cout << "Pleas enter the desired Value" << endl;
        cin  >> ValueN;
        cout << endl;

        drawTriangle(ValueN);
        cout << endl;

        drawUpsideDownTriangle(ValueN);
        cout << endl;

        drawLeftAdjTriangle(ValueN);
        cout << endl;

        drawLeftAdjUpsideDownTriangle(ValueN);
        cout << endl;

        getch();


}


That should do it.
Next time you should make your homework by yourself. ;3
Last edited on
Thank you. Actually its not homework. I am learning by myself.
Pattern 1:

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
/*
Description: Left align, inverted right angle triangle.
*/

#include <iostream>

using namespace std;

int main()
{
	int n;

	cout<<"Enter a number: ";

	cin>>n;

	for (int a = n; a > 0; a--)
	{
		for (int b = 1; b <= a; b++)
		{
			cout<<b<<" ";
		}

		cout<<endl;
	}

	return 0;
}



Pattern 2:

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
32
/*
Description: Right align, right angle triangle.
*/
#include <iostream>

using namespace std;

int main()
{
	int n;

	cout<<"Enter a number: ";

	cin>>n;

	for (int y = 1; y <= n; y++)
	{
		for(int s = n-y; s>0; s--)
		{
			cout<<"  "; //two spaces
		}

                for(int x = 1; x <= y; x++)
	       {
			cout<< x <<" ";
	       }

		cout << endl;
	}

	return 0;
}



Pattern 3:

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
32
33
/*
Description: Right align, inverted right angle triangle.
*/

#include <iostream>

using namespace std;

int main()
{
	int n;

	cout<<"Enter a number: ";

	cin>>n;

	for (int a = n; a > 0; a--)
	{
		for (int sp = 0; sp < n - a; sp++)
		{
			cout<<"  "; //two spaces
		}

		for(int b = 1; b <= a; b++)
		{
			cout<<b<<" ";
		}

		cout<<endl;
	}

	return 0;
}

Last edited on
Topic archived. No new replies allowed.