Name: Different Triangles
A triangle is one of the basic shapes of geometry: a polygon with three corners or vertices and three sides or edges which are line segments. A triangle with vertices A, B, and C is denoted ABC.
Description of the Program: This program displays the different types of triangle by entering their heights.
As you can see in the screenshot below, you are given the choice which type of triangle would you like to display. I entered letter a and the height of the triangle which is 6 in any units,then, it displayed the right triangle figure.
//Below are the Codes of the Program:
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
int z=1;
void right_tri( int a);
void up_right_tri( int a);
void up_left_tri( int a);
void left_tri( int a);
void inverse_vertical_tri( int a);
void vertical_tria(int a);
void vertical_diamond( int a);
void horizontal_diamond( int a);
void oposite_vertical( int a);
void oposite_horizontal( int a);
void input( int&a);
void input1( char&a);
void main()
{
int a,b,r;
char s;
input1(s);
input(a);
r=a;
if(s=='a')
right_tri(a);
else if(s=='b')
left_tri(a);
else if(s=='c')
up_right_tri(a);
else if(s=='d')
up_left_tri(a);
else if(s=='e')
inverse_vertical_tri(a);
else if(s=='f')
vertical_tria(a);
else if(s=='g')
vertical_diamond(r);
else if(s=='h')
horizontal_diamond(r);
else if(s=='i')
oposite_vertical(r);
else if(s=='j')
oposite_horizontal(r);
getch();
}
void input1( char&a)
{
cout<<"\nChoose what type of triangle would you like to display: ";
cout<<"\n\ta. A Right Triangle";
cout<<"\n\tb. A Left Triangle";
cout<<"\n\tc. An Up Right Triangle";
cout<<"\n\td. An Up Left Triangle";
cout<<"\n\te. A Reverse Vertical Triangle";
cout<<"\n\tf. A Vertical Triangle";
cout<<"\n\tg. A Vertical Diamond";
cout<<"\n\th. A Horizontal Diamond";
cout<<"\n\ti. An Opposite Vertical Triangle";
cout<<"\n\tj. An Opposite Horizontal Triangle\n";
a=getch();
cout<<" You have enter the letter of your choice.\n";
}
void input( int&a)
{
cout<<"Enter the height of the triangle:\n";
cin>>a;
}
void oposite_horizontal( int a)
{
cout<<"\n\Opposite Horizontal Triangle\n\n";
if(a%2==0)
{
for(int b=0;b<=(a/2)+1;b++)
{
for(int c=b;c>0;c--)
{
cout<<"*";
}
for(int y=b;y<=a/2;y++)
{
cout<<" ";
}
for(int e=b;e<=a/2;e++)
{
cout<<" ";
}
for(int zaa=b;zaa>0;zaa--)
{
cout<<"*";
}
cout<<"\n";
}
for(int x=1;x<=a/2;x++)
{
for(int y=x;y<=a/2;y++)
{
cout<<"*";
}
for(int za=x;za>0;za--)
{
cout<<" ";
}
for(int as=x;as>=1;as--)
{
cout<<" ";
}
for(int y=x;y<=a/2;y++)
{
cout<<"*";
}
cout<<"\n";
}
}
else
{
for(int b=0;b<=(a/2)+1;b++)
{
for(int c=b;c>0;c--)
{
cout<<"*";
}
for(int y=b;y<=a/2;y++)
{
cout<<" ";
}
for(int e=b;e<=a/2;e++)
{
cout<<" ";
}
for(int zaa=b;zaa>0;zaa--)
{
cout<<"*";
}
cout<<"\n";
}
for(int x=1;x<=a/2;x++)
{
for(int y=x;y<=a/2;y++)
{
cout<<"*";
}
for(int za=x;za>0;za--)
{
cout<<" ";
}
for(int as=x;as>=1;as--)
{
cout<<" ";
}
for(int y=x;y<=a/2;y++)
{
cout<<"*";
}
cout<<"\n";
}
}
}
void oposite_vertical( int a)
{
cout<<"\n\Opposite Vertical Triangle\n\n";
if(a%2==0)
{
for(int b=0;b<(a+1)/2;b++)
{
for(int c=b;c>0;c--)
{
cout<<" ";
}
for(int y=b;y<(a+1)/2;y++)
{
cout<<"*";
}
for(int e=b;e<(a-1)/2;e++)
{
cout<<"*";
}
cout<<"\n";
}
for(int x=1;x<=(a+1)/2;x++)
{
for(int y=x;y<(a+1)/2;y++)
{
cout<<" ";
}
for(int za=x;za>0;za--)
{
cout<<"*";
}
for(int as=x;as>1;as--)
{
cout<<"*";
}
cout<<"\n";
}
}
else
{
for(int b=1;b<(a+1)/2;b++)
{
for(int c=b-1;c>0;c--)
{
cout<<" ";
}
for(int y=b;y<=(a+1)/2;y++)
{
cout<<"*";
}
for(int e=b;e<=(a-1)/2;e++)
{
cout<<"*";
}
cout<<"\n";
}
for(int x=1;x<=(a+1)/2;x++)
{
for(int y=x;y<(a+1)/2;y++)
{
cout<<" ";
}
for(int za=x;za>0;za--)
{
cout<<"*";
}
for(int as=x;as>1;as--)
{
cout<<"*";
}
cout<<"\n";
}
}
}
void horizontal_diamond( int a)
{
cout<<"\n\Horizontal Diamond\n\n";
for(int x=1;x<=(a+1)/2;x++)
{
for(int y=x;y<(a+1)/2;y++)
{
cout<<"\t";
}
for(int za=x;za>0;za--)
{
cout<<"\t*";
}
for(int as=x;as>1;as--)
{
cout<<"\t*";
}
cout<<"\n";
}
for(int b=1;b<(a+1)/2;b++)
{
for(int c=b;c>0;c--)
{
cout<<"\t";
}
for(int y=b;y<(a+1)/2;y++)
{
cout<<"\t*";
}
for(int e=b;e<(a-1)/2;e++)
{
cout<<"\t*";
}
cout<<"\n";
}
}
void vertical_diamond( int a)
{
cout<<"\n\nVertical Diamond\n\n";
for(int x=1;x<=(a+1)/2;x++)
{
for(int y=x;y<(a+1)/2;y++)
{
cout<<" ";
}
for(int za=x;za>0;za--)
{
cout<<"*";
}
for(int as=x;as>1;as--)
{
cout<<"*";
}
cout<<"\n";
}
for(int b=1;b<(a+1)/2;b++)
{
for(int c=b;c>0;c--)
{
cout<<" ";
}
for(int y=b;y<(a+1)/2;y++)
{
cout<<"*";
}
for(int e=b;e<(a-1)/2;e++)
{
cout<<"*";
}
cout<<"\n";
}
}
void vertical_tria(int a)
{
int y=1;
cout<<"\n\nVertical Triangle\n\n";
for(int i=1;i<=a;i++)
{
for(int s=a;s>=i;s--)
{
cout<<" ";
}
for(int a=0;a<y;a++)
{
cout<<"*";
}
y+=2;
cout<<"\n";
}
}
void inverse_vertical_tri(int a)
{
cout<<"\n\nInverse Vertical Triangle\n\n";
int x,y,z,g,b,c,d,e;
for(b=1;b<=a;b++)
{
for(c=b;c>0;c--)
{
cout<<" ";}
for(g=b;g<=a;g++)
{
cout<<"*";
}
for(e=b;e<=a-1;e++)
{
cout<<"*";
}
cout<<"\n";
}
}
void up_left_tri( int a)
{
cout<<"\n\nUp Right Triangle Facing Left\n\n";
for(int xa=1;xa<=a;xa++)
{
for(int x=1;x<z;x++)
{
cout<<" ";
}
for(int xz=xa;xz<=a;xz++)
{
cout<<"*";
}
z++;
cout<<"\n";
}
}
void up_right_tri( int a)
{
cout<<"\n\nUp Right Triangle Facing Right\n\n";
for(int xa=1;xa<=a;xa++)
{
for(int x=a;x>=xa;x--)
{
cout<<"*";
}
for(int xz=0;xz<=z;xz++)
{
cout<<" ";
}
z++;
cout<<"\n";
}
}
void left_tri( int a)
{
cout<<"\n\nLeft Triangle\n\n";
for(int xa=1;xa<=a;xa++)
{
for(int x=a;x>xa;x--)
{
cout<<" ";
}
for(int xz=0;xz<z;xz++)
{
cout<<"*";
}
z++;
cout<<"\n";
}
}
void right_tri( int a)
{
cout<<"\n\nRight Triangle\n\n";
for(int xa=0;xa<=a;xa++)
{
for(int x=0;x<xa;x++)
{
cout<<"*";
}
cout<<"\n";
}
}
#include<conio.h>
#include<iostream.h>
int z=1;
void right_tri( int a);
void up_right_tri( int a);
void up_left_tri( int a);
void left_tri( int a);
void inverse_vertical_tri( int a);
void vertical_tria(int a);
void vertical_diamond( int a);
void horizontal_diamond( int a);
void oposite_vertical( int a);
void oposite_horizontal( int a);
void input( int&a);
void input1( char&a);
void main()
{
int a,b,r;
char s;
input1(s);
input(a);
r=a;
if(s=='a')
right_tri(a);
else if(s=='b')
left_tri(a);
else if(s=='c')
up_right_tri(a);
else if(s=='d')
up_left_tri(a);
else if(s=='e')
inverse_vertical_tri(a);
else if(s=='f')
vertical_tria(a);
else if(s=='g')
vertical_diamond(r);
else if(s=='h')
horizontal_diamond(r);
else if(s=='i')
oposite_vertical(r);
else if(s=='j')
oposite_horizontal(r);
getch();
}
void input1( char&a)
{
cout<<"\nChoose what type of triangle would you like to display: ";
cout<<"\n\ta. A Right Triangle";
cout<<"\n\tb. A Left Triangle";
cout<<"\n\tc. An Up Right Triangle";
cout<<"\n\td. An Up Left Triangle";
cout<<"\n\te. A Reverse Vertical Triangle";
cout<<"\n\tf. A Vertical Triangle";
cout<<"\n\tg. A Vertical Diamond";
cout<<"\n\th. A Horizontal Diamond";
cout<<"\n\ti. An Opposite Vertical Triangle";
cout<<"\n\tj. An Opposite Horizontal Triangle\n";
a=getch();
cout<<" You have enter the letter of your choice.\n";
}
void input( int&a)
{
cout<<"Enter the height of the triangle:\n";
cin>>a;
}
void oposite_horizontal( int a)
{
cout<<"\n\Opposite Horizontal Triangle\n\n";
if(a%2==0)
{
for(int b=0;b<=(a/2)+1;b++)
{
for(int c=b;c>0;c--)
{
cout<<"*";
}
for(int y=b;y<=a/2;y++)
{
cout<<" ";
}
for(int e=b;e<=a/2;e++)
{
cout<<" ";
}
for(int zaa=b;zaa>0;zaa--)
{
cout<<"*";
}
cout<<"\n";
}
for(int x=1;x<=a/2;x++)
{
for(int y=x;y<=a/2;y++)
{
cout<<"*";
}
for(int za=x;za>0;za--)
{
cout<<" ";
}
for(int as=x;as>=1;as--)
{
cout<<" ";
}
for(int y=x;y<=a/2;y++)
{
cout<<"*";
}
cout<<"\n";
}
}
else
{
for(int b=0;b<=(a/2)+1;b++)
{
for(int c=b;c>0;c--)
{
cout<<"*";
}
for(int y=b;y<=a/2;y++)
{
cout<<" ";
}
for(int e=b;e<=a/2;e++)
{
cout<<" ";
}
for(int zaa=b;zaa>0;zaa--)
{
cout<<"*";
}
cout<<"\n";
}
for(int x=1;x<=a/2;x++)
{
for(int y=x;y<=a/2;y++)
{
cout<<"*";
}
for(int za=x;za>0;za--)
{
cout<<" ";
}
for(int as=x;as>=1;as--)
{
cout<<" ";
}
for(int y=x;y<=a/2;y++)
{
cout<<"*";
}
cout<<"\n";
}
}
}
void oposite_vertical( int a)
{
cout<<"\n\Opposite Vertical Triangle\n\n";
if(a%2==0)
{
for(int b=0;b<(a+1)/2;b++)
{
for(int c=b;c>0;c--)
{
cout<<" ";
}
for(int y=b;y<(a+1)/2;y++)
{
cout<<"*";
}
for(int e=b;e<(a-1)/2;e++)
{
cout<<"*";
}
cout<<"\n";
}
for(int x=1;x<=(a+1)/2;x++)
{
for(int y=x;y<(a+1)/2;y++)
{
cout<<" ";
}
for(int za=x;za>0;za--)
{
cout<<"*";
}
for(int as=x;as>1;as--)
{
cout<<"*";
}
cout<<"\n";
}
}
else
{
for(int b=1;b<(a+1)/2;b++)
{
for(int c=b-1;c>0;c--)
{
cout<<" ";
}
for(int y=b;y<=(a+1)/2;y++)
{
cout<<"*";
}
for(int e=b;e<=(a-1)/2;e++)
{
cout<<"*";
}
cout<<"\n";
}
for(int x=1;x<=(a+1)/2;x++)
{
for(int y=x;y<(a+1)/2;y++)
{
cout<<" ";
}
for(int za=x;za>0;za--)
{
cout<<"*";
}
for(int as=x;as>1;as--)
{
cout<<"*";
}
cout<<"\n";
}
}
}
void horizontal_diamond( int a)
{
cout<<"\n\Horizontal Diamond\n\n";
for(int x=1;x<=(a+1)/2;x++)
{
for(int y=x;y<(a+1)/2;y++)
{
cout<<"\t";
}
for(int za=x;za>0;za--)
{
cout<<"\t*";
}
for(int as=x;as>1;as--)
{
cout<<"\t*";
}
cout<<"\n";
}
for(int b=1;b<(a+1)/2;b++)
{
for(int c=b;c>0;c--)
{
cout<<"\t";
}
for(int y=b;y<(a+1)/2;y++)
{
cout<<"\t*";
}
for(int e=b;e<(a-1)/2;e++)
{
cout<<"\t*";
}
cout<<"\n";
}
}
void vertical_diamond( int a)
{
cout<<"\n\nVertical Diamond\n\n";
for(int x=1;x<=(a+1)/2;x++)
{
for(int y=x;y<(a+1)/2;y++)
{
cout<<" ";
}
for(int za=x;za>0;za--)
{
cout<<"*";
}
for(int as=x;as>1;as--)
{
cout<<"*";
}
cout<<"\n";
}
for(int b=1;b<(a+1)/2;b++)
{
for(int c=b;c>0;c--)
{
cout<<" ";
}
for(int y=b;y<(a+1)/2;y++)
{
cout<<"*";
}
for(int e=b;e<(a-1)/2;e++)
{
cout<<"*";
}
cout<<"\n";
}
}
void vertical_tria(int a)
{
int y=1;
cout<<"\n\nVertical Triangle\n\n";
for(int i=1;i<=a;i++)
{
for(int s=a;s>=i;s--)
{
cout<<" ";
}
for(int a=0;a<y;a++)
{
cout<<"*";
}
y+=2;
cout<<"\n";
}
}
void inverse_vertical_tri(int a)
{
cout<<"\n\nInverse Vertical Triangle\n\n";
int x,y,z,g,b,c,d,e;
for(b=1;b<=a;b++)
{
for(c=b;c>0;c--)
{
cout<<" ";}
for(g=b;g<=a;g++)
{
cout<<"*";
}
for(e=b;e<=a-1;e++)
{
cout<<"*";
}
cout<<"\n";
}
}
void up_left_tri( int a)
{
cout<<"\n\nUp Right Triangle Facing Left\n\n";
for(int xa=1;xa<=a;xa++)
{
for(int x=1;x<z;x++)
{
cout<<" ";
}
for(int xz=xa;xz<=a;xz++)
{
cout<<"*";
}
z++;
cout<<"\n";
}
}
void up_right_tri( int a)
{
cout<<"\n\nUp Right Triangle Facing Right\n\n";
for(int xa=1;xa<=a;xa++)
{
for(int x=a;x>=xa;x--)
{
cout<<"*";
}
for(int xz=0;xz<=z;xz++)
{
cout<<" ";
}
z++;
cout<<"\n";
}
}
void left_tri( int a)
{
cout<<"\n\nLeft Triangle\n\n";
for(int xa=1;xa<=a;xa++)
{
for(int x=a;x>xa;x--)
{
cout<<" ";
}
for(int xz=0;xz<z;xz++)
{
cout<<"*";
}
z++;
cout<<"\n";
}
}
void right_tri( int a)
{
cout<<"\n\nRight Triangle\n\n";
for(int xa=0;xa<=a;xa++)
{
for(int x=0;x<xa;x++)
{
cout<<"*";
}
cout<<"\n";
}
}