Flip Pattern Half Pyramid in C++

C++C++Beginner
Practice Now

Introduction

In this lab, we will be creating a program in C++ to print a flip pattern of a half pyramid using * symbol. We will be printing the pattern in such a way that the number of stars in each row starts with the maximum number and then decreases by one in each consecutive row.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/IOandFileHandlingGroup(["`I/O and File Handling`"]) cpp(("`C++`")) -.-> cpp/SyntaxandStyleGroup(["`Syntax and Style`"]) cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp/IOandFileHandlingGroup -.-> cpp/output("`Output`") cpp/SyntaxandStyleGroup -.-> cpp/comments("`Comments`") cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/IOandFileHandlingGroup -.-> cpp/user_input("`User Input`") cpp/BasicsGroup -.-> cpp/data_types("`Data Types`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") subgraph Lab Skills cpp/output -.-> lab-96206{{"`Flip Pattern Half Pyramid in C++`"}} cpp/comments -.-> lab-96206{{"`Flip Pattern Half Pyramid in C++`"}} cpp/variables -.-> lab-96206{{"`Flip Pattern Half Pyramid in C++`"}} cpp/user_input -.-> lab-96206{{"`Flip Pattern Half Pyramid in C++`"}} cpp/data_types -.-> lab-96206{{"`Flip Pattern Half Pyramid in C++`"}} cpp/operators -.-> lab-96206{{"`Flip Pattern Half Pyramid in C++`"}} cpp/for_loop -.-> lab-96206{{"`Flip Pattern Half Pyramid in C++`"}} end

Creating a new file

First, create a new file named flip_pattern_half_pyramid.cpp in the ~/project directory by running the following command in the terminal:

touch ~/project/flip_pattern_half_pyramid.cpp

Including Header files

We will start by including the necessary header files in our code.

#include <iostream>

The iostream header file contains the standard input and output functions in C++.

Creating the main() function

This is the main function of our program.

int main()
{
   // Code will come here
   return 0;
}

Declaring variables

We will declare the rows variable to store the number of rows, which will be entered by the user later.

int rows;

Asking for user input

We will ask the user to enter the number of rows they want for the pattern.

std::cout << "Enter the number of rows: ";
std::cin >> rows;

Creating Loops

Here, we will be using two nested loops to print out the flip pattern of half pyramid.

for(int i = rows; i >= 1; --i)
{
    for(int j = 1; j <= i; ++j)
    {
        std::cout << "* ";
    }
    std::cout << std::endl;
}

Combining the code

Combine all the above code to create the following code:

#include<iostream>

int main()
{
    int rows;

    std::cout<<"Enter the number of rows: ";
    std::cin>>rows;

    for(int i = rows; i >= 1; --i)
    {
        for(int j = 1; j <= i; ++j)
        {
            std::cout << "* ";
        }
        std::cout << std::endl;
    }

    return 0;
}

Running the code

To run the above code, follow the below steps:

  1. Open your terminal.

  2. Navigate to the directory where your flip_pattern_half_pyramid.cpp file is located.

  3. Compile the code using the following command:

    g++ flip_pattern_half_pyramid.cpp -o flip_pattern_half_pyramid
  4. Run the code using the following command:

    ./flip_pattern_half_pyramid
  5. Enter the number of rows you want for the pattern when prompted by the program.

Output

After running the program, you will see the output on your terminal screen.

Enter the number of rows: 5
* * * * *
* * * *
* * *
* *
*

Summary

In this lab, we successfully created a C++ program to print out the flip pattern of half pyramid using the * symbol. We achieved this by using nested loops with basic syntax to build the program.

Other C++ Tutorials you may like