Program #1 Write a program that asks the user to enter an integer between 1 and
ID: 3806461 • Letter: P
Question
Program #1
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. (This is an opportunity to write an input validation loop.)
Once the user enters a number in the correct range, your program will print out four patterns of asterisks that look like the following. Note that the number provided by the user is equal to the height of the pattern.
Example- If the input value is 5, please print the following four patterns:
* * * * *
* * * *
* * *
* *
*
* * * * *
* * * *
* * *
* *
*
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
*
* *
* *
* *
* * * * * * * * *
As you can see, the input value represents the height of the triangle.
Your program should only print one asterisk at a time and use loops to control the number of asterisks printed per line.
PLEASE CREATE PROGRAM IN C++ AND PUT ALL COONTENTS AND LOOPS INTO ONE SINGLE FUNCTION
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
void pattern(int n3);
void pattern(int n3) {
int i,j,k;
for(i=0; i<n3; i++)
{
for(j=n3; j>i; j--)
{
cout<<"* ";
}
cout<<" ";
}
i=0,j=0,k=0;
for(i=1; i<=n3; i++)
{
for(j=n3-1; j>=i; j--)
{
cout<<" ";
}
for(k=1; k<=(2*i-1); k++)
{
cout<<"*";
}
cout<<" ";
}
}
int main()
{
int n1;
cout << "Enter a number between 1-15";
cout << endl;
cin>> n1;
cout << endl;
if(n1>=1 && n1<=15){
pattern(n1);
}else{
cout << "The number is not between 1-15";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.