Question

C++. Write a program that uses nested while loops or for statements to display Pattern A...

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

+++++++++++++
+++++++++++
+++++++++
+++++++
+++++
+++
+

Homework Answers

Answer #1
#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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C programming 3. Write a do-while loop that displays: 0,10,20,30,40 4.Write nested for loops that displays...
C programming 3. Write a do-while loop that displays: 0,10,20,30,40 4.Write nested for loops that displays three rows of asterisks: ***** ***** ***** 5. Write a complete program that reads a set of integers using scant() until a sentinel value of -99 is read. Once that value is read, display the sum of the value read, exclusive of the -99. 6.Write a procedure(function, method named times10() that accepts an integer argument and displays the result of multiplying the argument by...
In C++ write a program for a multiplication table It must Prompt the user for two...
In C++ write a program for a multiplication table It must Prompt the user for two integers between 1 and 20 (inclusive) • also must Calculates and displays the multiplication table in a well-formatted output The table must include a label for each row and column The program must follow the requirements: • Validates user input, displaying an error message and prompting to user to enter another integer if the input is invalid, and repeating it as many times as...
In this exercise we will practice using loops to handle collections/containers. Your job is to write...
In this exercise we will practice using loops to handle collections/containers. Your job is to write a program that asks the user to enter a sentence, then it counts and displays the occurrence of each letter. Note: your program should count the letters without regard to case. For example, A and a are counted as the same. Here is a sample run: Enter a sentence: It's a very nice day today! a: 3 times c: 1 times d: 2 times...
C++. Write a program that draws a rocket shape on the screen based on user input...
C++. Write a program that draws a rocket shape on the screen based on user input of three values, height, width and stages. The type of box generated (i.e.. a hollow or filled-in) is based on a check for odd or even values input by the user for the box height (or number of rows). Here is the general specification given user input for the height of the box..... Draw a hollow box for each stage if the value for...
Objective: Write a Java program that will use a JComboBox from which the user will select...
Objective: Write a Java program that will use a JComboBox from which the user will select to convert a temperature from either Celsius to Fahrenheit, or Fahrenheit to Celsius. The user will enter a temperature in a text field from which the conversion calculation will be made. The converted temperature will be displayed in an uneditable text field with an appropriate label. Specifications Structure your file name and class name on the following pattern: The first three letters of your...