C++. Write a program that uses nested while loops or for statements to display Pattern A below, followed by an empty line and then another set of loops that displays Pattern B. Once again no setw implementation is allowed here and you must use appropriate control structures with proper indentation to generate these static patterns. Use meaningful variable identifier names, good prompting messages and appropriate comments for each loop segment as well as other sections in the body of your program.
Note: Each triangle has exactly seven rows and the width of each row is an odd number. The triangles should be displayed one after the other with appropriate labels . No user input is required.
Pattern A
+
+++
+++++
+++++++
+++++++++
+++++++++++
+++++++++++++
Pattern B
+++++++++++++
+++++++++++
+++++++++
+++++++
+++++
+++
+
#include <iostream> // input output stream header file
using namespace std; // input output standards
int main() // main function
{
//cout used to print statements on screen
cout<<"Pattern A"<<endl;
for (int i = 0; i < 7; i++) {
// used to print rows of our pattern A
for (int j = 0; j < 2*i + 1; j++) {
// inside each row we have 1 "+" then 3 "+" then 5 "+"
// therefore observing pattern as 2*row + 1
cout<<"+";
// used to print our character "+"
}
cout<<endl;
// after each line printing we insert new line, endl manipulator is similar to '\n'
}
cout<<"Pattern B"<<endl;
//as we want number of character printed decreases in each row we start loop from 6 till 0
for (int i = 6; i >= 0 ; i--) {
// used to print rows of our pattern B
for (int j = 0; j < 2*i+1; j++) {
// inside each row we have 13 "+" then 11 "+" then 9 "+"
// therefore observing pattern as 2*row + 1
cout<<"+";
// used to print our character "+"
}
cout<<endl;
// after each line printing we insert new line, endl manipulator is similar to '\n'
}
return 0; //return type of main is int
}
code is working try online compiler over the internet.
also i have used comments so that each line can be better understood.
Get Answers For Free
Most questions answered within 1 hours.