Write a C++ program that performs the following tasks:
1. Prompts the user to enter a positive integer that is less than 20.
2. Inputs an integer and validates that it is positive and less than 20.
3. Outputs four triangles with the following properties:
All four triangles are right triangles
Each triangle has a different orientation.
If n is the input number, the last row of the triangles has n *'s in the largest row.
For example, if the number input is 4, the program should output the following triangles
a. *
**
***
****
b. ****
***
**
*
c. ****
***
**
*
d. *
**
***
****
4, Outputs the same four triangles right next to each other.
For example, if the input number is 5, the following pattern should be output:
* ********** *
** **** **** **
*** *** *** ***
**** ** ** ****
****** ******
5. Allows the user to repeat the program as many times as they desire with different input.
Objective:
To gain practice in programming loops
Display:
Enter a postive integer that is less than 20: 0
You entered a non valid input. Try again
Enter a postive integer: 20
You entered a non valid input. Try again
Enter a postive integer: 7
*
**
***
****
*****
******
*******
*******
******
*****
****
***
**
*
*******
******
*****
****
***
**
*
*
**
***
****
*****
******
*******
* ************** *
** ****** ****** **
*** ***** ***** ***
**** **** **** ****
***** *** *** *****
****** ** ** ******
******** ********
Would you like to try again (y/n): n
End of Program
Code:
#include
using namespace std;
int main ()
{
//declaration of variables
int row, num = 1, num2 = 1, num3 = 1, num4 = 1,num5 = 1, num6 = row - num4, num7 = row - num4 + 1, num8 = 1;
char choice;
do
{
cout <<"\nEnter a postive integer that is less than 20: ";
cin >> row;
cout << endl;
while(row <= 0 || row >= 20)
{
cout <<"\n\nYou entered a non valid input. Try again";
cout <<"\nEnter a postive integer: ";
cin >> row;
cout << endl;
}
for(num=1;num<=row;num++)
{
for(num2 = 1; num2 <= num; num2++)
{
cout << "*";
}
cout << endl;
}
cout << endl;
for(num = row; num >= 1;num--)
{
for(num2 = 1; num2 <= num ; num2++)
{
cout << "*";
}
cout << endl;
}
cout << endl;
for(num=1;num<=row;num++)
{
for(num2 = num; num2 >=1; num2--)
{
for(num2 = num; num2 >1 ; num2--)
{
cout << " ";
}
for(num3 = num; num3 <= row; num3++)
{
cout << "*";
}
}
cout << endl;
}
cout << endl;
for(num=1;num<=row;num++)
{
for(num2 = num; num2 >=1; num2--)
{
for(num3 = num; num3 < row; num3++)
{
cout << " ";
}
for(num2 = num; num2 >=1 ; num2--)
{
cout << "*";
}
}
cout << endl;
}
cout << endl;
for(num4=1; num4<= row; num4++)
{
for(num5=1;num5<=num4;num5++)
cout<<"*";
for(num6=row-num4;num6>=1;num6--)
cout<<" ";
for(num7=row-num4+1;num7>=1;num7--)
cout<< "*";
for(num8=1;num8
for(num5=1;num5
for(num7=row-num4+1;num7>=1;num7--)
cout<< "*";
for(num7=row-num4+1;num7>1;num7--)
cout<< " ";
for(num8=1;num8<=num4;num8++)
cout<<"*";
cout << endl;
}
cout<<"\nWould you like to try again (y/n): ";
cin>>choice;
}
while(choice =='y' || choice =='Y');
cout << "\nEnd of Program\n\n";
return 0;
}