All Types Of Triangle Star Pattern Program In C++
Joining in any IT
company is still a problem for every candidate. So if you are attending an
interview in a company then you should concentrate on grabbing the offer letter
as soon as possible.
Basic
programming question of all time is pattern programming. You are supposed to do
this in any language. So we are going to discuss deeply about all the pattern
programming models and the answers. This will help you to know all the pattern
related programming problems.
Type 1 (Star Pattern Right Angled Triangle):
Output :
Source Code :
#include <iostream>
using namespace std;
int main()
{
int rows;
cout<<"Enter the number of rows : ";
cin>>rows;
for(int a=1;a<=rows;a++)
{
for(int b=1;b<=a;b++)
{
cout<<" * ";
}
cout<<"\n";
}
return 0;
}
Type 2 (Mirrored Star Pattern Right Angled Triangle / Left Angled Triangle):
Output :
Source Code :
#include <iostream>
using namespace std;
int main()
{
int
rows, a, b;
cout<<"Enter the Number of Rows: ";
cin>>rows;
for
( a = 1 ; a <= rows; a++ )
{
for ( b = 1 ; b <= rows; b++ )
{
if (b <= rows-a)
{
cout<<" ";
}
else
{
cout<<"*";
}
}
cout<<"\n";
}
return 0;
}
Type 3 (Flipped Down / Inverted Right Angled Triangle):
Output :
Source Code :
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int a = rows; a >= 1; --a)
{
for(int b = 1; b <= a; ++b)
{
cout << "* ";
}
cout << endl;
}
return 0;
}
Output :
Source Code :
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int a = rows; a >= 1; a--)
{
for(int b = rows-1; b >= a; b--)
{
cout<<" ";
}
for(int c = 1; c <= a; c++)
{
cout<<"*";
}
cout << endl;
}
return 0;
}
Comments
Post a Comment