Right angle triangles.
Oct 1, 2010 at 10:01am UTC  
A program that displays a right angle triangle pattern.
1#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 Oct 1, 2010 at 4:21pm UTC  
 
Oct 1, 2010 at 11:01am UTC  
Pattern 1,
 
Oct 1, 2010 at 1:01pm UTC  
can u please post the codes. u see i am just a beginner.
 
Oct 1, 2010 at 1:58pm UTC  
1#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 Oct 1, 2010 at 2:07pm UTC  
 
Oct 1, 2010 at 3:16pm UTC  
Thank you. Actually its not homework. I am learning by myself.
 
Oct 1, 2010 at 4:15pm UTC  
Pattern 1:
1/*
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/*
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/*
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 Oct 1, 2010 at 4:20pm UTC  
 
Topic archived. No new replies allowed.