Ask the user for an integer between 1 and 99. Based on the user input print the
ID: 3535462 • Letter: A
Question
Ask the user for an integer between 1 and 99. Based on the user input print the following patterns. NOTE: You may not hard code the patterns and you MUST use loops to print them. Otherwise no credit will be given for the problem.
For example, if the user enters 7 the patterns will look like:
Pattern 1:
+++++++
+++++++
+++++++
+++++++
+++++++
+++++++
+++++++
Pattern 2:
+
++
+++
++++
+++++
++++++
+++++++
Pattern 3:
+++++++
++++++
+++++
++++
+++
++
+
If the user enters 10 the patterns will look like:
Pattern 1:.
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
++++++++++
Pattern 2:
+
++
+++
++++
+++++
++++++
+++++++
++++++++
+++++++++
++++++++++
Pattern 3:
++++++++++
+++++++++
++++++++
+++++++
++++++
+++++
++++
+++
++
+
Explanation / Answer
#include <iostream>
using namespace std;
int main(){
int n;
cout << "Printes three patterns."<<endl << endl;
cout << "Enter a number: ";
cin >> n;
cout << "Pattern 1:" << endl << endl;
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
cout << '+';
}
cout << endl;
}
cout << "Pattern 2:" << endl << endl;
for(int i = 0; i < n; ++i){
for(int j = 0; j <= i; ++j){
cout << '+';
}
cout << endl;
}
cout << "Pattern 2:" << endl << endl;
for(int i = n; i > 0; --i){
for(int j = 0; j < i; ++j){
cout << '+';
}
cout << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.