Write a program that behaves as shown below: Here are the rules: Your program mu
ID: 3686512 • Letter: W
Question
Write a program that behaves as shown below:
Here are the rules:
Your program must use at least one for-loop.
Your program must output the parallelogram just one character at a time, inside a loop. (You will need nested loops.)
Your program must work with any length greater than 1. (You don't need to do any error checking for bad input)
Your program must use the character the user inputs to draw the parallelogram.
Hint: I recommend you think of this as a program to output two triangles, one above the other (the longest horizontal line is the border between the triangles).
Hint #2: To make this easier to solve, start by writing a program that outputs this text for the last example above:
Explanation / Answer
#include<iostream>
using namespace std;
int main(){
int n;
char c;
cout<<"This program will output a parallelogram. How long do you want each side to be?";
cin>>n;
cout<<"Please enter the character you want it to be made of:";
cin>>c;
for (int i=0;i<n;i++){
for (int j=0;j<=i;j++){
cout<<c;
}
cout<<endl;
}
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
if (j<=i){
cout<<" ";
} else cout<<c;
}
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.