×

Star pattern in C++ using For Loops

Star patterns are one of the most extensively utilized patterns in any programming language since they help to increase logical thinking and flow control understanding.
In the C++ programming language, you need two or three loops to generate a pattern. The number of loops you require depends on the pattern you want to make. A minimum of two patterns are used, first for each row and the second for each column. The first loop, known as an outer loop which display rows, while the second loop, known as an inner loop which display columns.

What is For Loop?

A for loop is a repetitive control structure that allows you to create a loop for executing a specific number of times.

The syntax of For Loop

In C++, a for loop is written as

for ( initialize; condition; incrementation ) 
 {
 statement(x);
 }

The programmer or user inserts the number of rows according to how many stars he wants to print.

1st pattern:  Program in C++ to print half star pyramid pattern:

#include <iostream>
using namespace std;
int main()
{
int  i, j, n;
cout << "Enter number of rows:  ";
cin >> n;
for(i = 1; i <= n; i++)     // outer loop(to print rows)
        {
            for(j = 1; j <= i; j++)      //inner loop(to print columns)
                  {
                       cout << "* ";
                  }
//Ending line after each row //
           cout << "\n";
          }
return 0;
}

Output:

Enter number of rows:  4
*
*  *
*  *  *
*  *  *  *

2nd pattern: Program in C++ to print inverted half star pyramid pattern

#include <iostream>
using namespace std;
int main()
{
int  i, j, n;
cout << "Enter number of rows:  ";
cin >> n;
for(i = n; i >= 1; i--)
      {
             for(j = 1; j <= i; j++)
                   {
                         cout << "* ";
                   }
// ending line after each row
      cout << "\n";
        }
return 0;
}


Output:

*   *   *   *
*   *   *
*   *
*

3rd pattern: Program in C++ to print star pyramid pattern:

#include<iostream>
using namespace std;
int main()
{
int n, s, i, j;
cout << "Enter number of rows: ";
cin >> n;
for(i = 1; i <= n; i++)
       {
             //for loop for displaying space
             for(s = i; s < n; s++)
                    {
                         cout << " ";
                     }
              //for loop to display star equal to the row number
           for(j = 1; j <= (2 * i - 1); j++)
                   {
                       cout << "*";
                   }
// ending line after each row
cout << "\n";
}
}

Output:

Enter the number of rows: 4
              *
            * * *
          * * * * *
        * * * * * * *

4th pattern: Program in C++ to print inverted star pyramid pattern:

#include<iostream>
using namespace std;
int main()
{
int n, s, i, j;
cout << "Enter number of rows: ";
cin >> n;
for( i=n; i>= 1; i-- )
{
//for loop to put space
     for(s = i; s < n; s++)
          {
               cout << " ";
          }
        //for loop for displaying star
     for(j = 1; j <= i; j++)
          {
                cout << "* ";
            }
// ending line after each row
cout << "\n";
}
return 0;
}

Output:

Enter number of rows: 4


*   *   *   *
  *  *   *
    *  *
      *

5th  pattern: Program to print full star diamond pattern in C++:

#include<iostream>
using namespace std;
int main()
{
int n, s, i, j;
cout << "Enter number of rows: ";
cin >> n;
  //loop to print the upper diamond pattern //
for(i = 0; i <= n; i++)
{
for(s = n; s > i; s--)
cout << " ";
for(j=0; j<i; j++)
cout << "* ";
cout << "\n";
}
// for loop to print the inverted diamond pattern //
for(i = 1; i < n; i++)
{
for(s = 0; s < i; s++)
cout << " ";
for(j = n; j > i; j--)
cout << "* ";
// ending line after each row
cout << "\n";
}
return 0;
}

Output:

Enter number of rows: 7             
              *
            *  *
         *   *    *
      *    *    *    *
         *    *    *
            *     *
               * 

Related Topics

Default Constructor

C++ Default Constructor A default constructor is such constructor which does not take any parameter. A constructor initializes the data member when a class object created. If a programmer does not create any...

2 minutes read.

Floating Point Operations and Associativity in C, C++ and Java

In this tutorial, we are going to compare Floating-point operations and the concept of associativity. Before we apply the concept of associativity in the floating-point operations in all three programming...

3 minutes read.

Features of OOPS in C++

What is OOPs? The main reason programmers prefer C ++ language over C is because of the support of object-oriented programing in C++. As the name suggests, object-oriented programming or OOPs...

3 minutes read.

C++ if-else

C++ if else Control Statement if else control statement in C++ is used to control the program flow in a two-way direction. When condition returns a true value, then the program executes if condition block otherwise...

1 minute read.

How to Handle Divide by Zero Exception in C++

If you are a programmer or interested in coding then it is obvious that you face some illogical test cases. Suppose, you have written one program that calculates the factorial...

6 minutes read.

Implementing the sets without C++ STL containers

Many practical features and tools in C++ support us in programming competitions. One of these parts is a set from the Standard Template Library (STL), which offers an effective way...

6 minutes read.

Observer Design Pattern in C++

The Observer design pattern is a behavioural design pattern that allows an object (known as the subject) to notify other objects (known as observers) when its state changes. This is...

3 minutes read.

Implementation of a Falling Matrix in C++

In many Hollywood and Bollywood movies, we might have seen a programmer who will be referred to as a hacker all the time for some random reason which the writer...

3 minutes read.

Constructor Overloading in C++

A Constructor is a class member function that is used to initialize the class's objects. Constructors have no return type and are called automatically when an object is formed. Constructor Characteristics Constructors...

3 minutes read.

C++ Program to Implement Shell Sort

    C++ Program to Implement Shell Sort shell sort is basically an Insertion Sort variant. In the insertion sort, we only transfer elements ahead of one location. Many movements are involved...

2 minutes read.

C++ Inheritance

What do you mean by Inheritance ? The ability to define new classes based on existing classes in order to reuse and organise code is referred to as inheritance. Single inheritance...

7 minutes read.

Find the Size of Array in C/C++ without using sizeof() function

We know that arrays in C/C++ are the most essential data structures as they have the ability to hold the data in a continuous manner line where the address of...

3 minutes read.

C++ History

C++ is a middle-level programming language developed in 1980s by Bjarne Stroustrup at Bel Labs. C ++ development initially started in 1979, four years before its launch. It is started with...

1 minute read.

Storage Classes in C

Storage Classes in C Storage Classes are used to define the variable and function property. These functionalities include basically the scope, accessibility, and lifetime that help us detect the existence of...

4 minutes read.

How to Sort an Array in C++

What is Sorting? Sorting is a process of arranging elements in sequential order, either numerically or alphabetically. The sorting of a numerical array can be accomplished using a variety of algorithms,...

4 minutes read.

C++ Virtual Function

A virtual function is such function which is declared inside the base class and redefined by the derive class. C++ uses a virtual keyword to make a function as a virtual function. The virtual...

1 minute read.

Dynamic Memory Allocation in C++

In some programming situations, the number of data items changes as the program is running, which is known as dynamic data or input. Consider a real-world situation where a program...

3 minutes read.

Desired Capabilities in Selenium Web Driver in C++

A class called Desired Capabilities is used to specify a set of fundamental requirements, such as browser, operating system, and version combinations.to carry out automated testing of a web application...

7 minutes read.

C++ References

C++ References An alias is a reference variable that is another name for a variable already in existence. If a relation is initialized with a variable, it is possible to use...

3 minutes read.

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector. A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same...

4 minutes read.