Write a program that asks the user to enter an integer between 1 and 15. If the number entered is outside that range, your program should print out an error message and re-prompt for another number.
Once the user enters a number in the correct range, your program will print out two patterns of asterisks that look like the following. Note that the number provided by the user is equal to the height of the pattern.
Let us say that the input value was 5. Then please print the following two patterns:
*****
****
***
**
*
*****
+****
++***
+++**
++++* (Without the +'s)
#include <iostream>
usingnamespace std;
int main()
{
int num;
do
{
cout << "Enter a number between 1 and 15: ";
cin >> num;
if (num > 15 || num < 1)
{
cout << "Error! The number must be between 1 and 15." << endl;
}
}
while (num > 15 || num < 1);
for(int pattern = 0; pattern < 2; pattern++)
{
for (int i = num; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
cout << "*";
}
cout << endl;
}
}
cin.get();
return 0;
}
Oh, I see.
By the way, you can use [output] [/output] tags so that your spaces won't get eaten up.
So, if I understand correctly, you want it to look like
*****
****
***
**
*
*****
****
***
**
*
?
It's similar to what you're doing right now for the first pattern, except before you print the stars, you should first print out a number of spaces equal to 5 - (how many stars you're supposed to print on this loop iteration).
Yeah, so for the second pattern, you should print out
0 spaces + 5 asterisks
1 space + 4 asterisks
2 spaces + 3 asterisks
3 spaces + 2 asterisks
4 spaces + 1 asterisk