×

Diamond Pattern using While-loop in C++

What is while Loop?

A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the condition no longer met.

The syntax that can be used for the “while” loop in the C++ programming language is following:

while(condition)
    {
     statement(x);
    }

Before we move on the diamond star pattern, first we have to know the code of star pyramid and inverted star pyramid.

1. Right Triangle Star Pyramid:

Algorithm:

  1. Assume that the number of rows in a right triangle is n.
  2. In the Nth row, the number of stars is always K. The first row has one star, the second row has two stars, and the third row has three stars. The Kth row, on average, has K stars.
  3. To print the proper triangle star design, we'll utilize two loops.
    1. The outer while loop will iterate n times for a right triangular star design with "n" rows. The pattern will be printed one row at a time in each outer loop iteration.
    2. The inner loop will also run n times for the Nth row of the right triangle pattern. One star (*) will be printed for each inner loop iteration.

In the code, programmer/user enters the number of rows he wants display the stars on the screen.

Program in C++ to print right triangle star pyramid pattern

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

Output

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

Inverted Right Triangle Star pyramid

Algorithm:

The right triangular star pattern is inverted vertically in this algorithm. As the row number goes from top to bottom, so the number of stars in a row reduces.

NOTE: The row and column indexes always start at 0.

  1. Take the inverted right triangle's number of rows (n) as input. In an inverted right triangle (n - k + 1) is the number of stars in the kth row. Let, n be 4 in the pattern. As a result, the first row has four stars, the second row has three stars, the third row has two stars, and the fourth row has only one star.
  2. To print an inverted right triangular star design, we'll use two while loops.
  3. The outer while loop will iterate n times (from I = 0 to n-1) for an inverted right triangular star design with M rows. The code will be printed one row at a time in each iteration of this loop.
  4.  The inner loop will iterate M-j times (from j = 0 to n-j) for the jth row of the inverted right triangle pattern. The inner loop will print one star character for each iteration.

Program to print Inverted right triangle star pyramid pattern

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

Output:

*   *   *   *
*   *   *
*   *
*

Pyramid star pattern:

Algorithm:

In this program, we print a pyramid star pattern with (2*i + 1) space-separated stars in the ith row.

The row and column indexes begin at 0.

The number of rows (N) in the pattern is the input taken by the user.

The outer loop will print a row of the pyramid (from I = 0 to N - 1) after one iteration.

The inner while loop prints (N - i - 1) spaces for every line in the jth row of the pyramid, then nested while loop prints (2*j + 1) stars.

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;
i=1;
while(i<=n)
           {
             //while loop for displaying space
            s=1;
          while(s<n) 
                       {
                         cout << " ";
                          s++;
                     }
              //while loop to display star equal to the row number
            j=1;
           while(j<=(2*i-1))
                    {
                       cout << "*";
                       j++;
                   }
// ending line after each row
cout << "\n";
i++;
}
}

Output

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

Inverted star pyramid:

Algorithm:

In this algorithm, the rows are printed in reverse order, which is comparable to the pyramid star pattern.

  1. The number of rows (n) in the pattern is the first input.
  2. a) In one iteration, the outer while loop (from I = 0 to n-1) will print a row of inverted pyramids.
    b) The inner while loops print "S" spaces followed by (2*(n-i) - 1) star character in the jth row.

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;
i=n;
while(i>=1)
{
      // loop to put space
     s=i;
  while(s<n)
            {
               cout << " ";
               s++;
           }
        //loop for displaying star
          j=1;
        While(j<=i)
             {
                cout << "* ";
            j++;
             }
// ending line after each row
 cout << "\n";
i--;
}
return 0;
}

Output

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

Diamond star pattern:

The diamond star pattern is the combination of the pyramid and inverted pyramid pattern. The following c++ program combines the code of simple pyramid and the code of reverse pyramid star.

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 //
i=0;
while(i<=n){
      s=n;
      while(s>i){
               cout << " ";
               s--;
               }
     j=0;
    while(j<i){
             cout << "* ";
             j++;
             }
cout << "\n";
i++;
}
           // loop to print the inverted diamond pattern //


i=1;
while(i<n){
       s = 0;
        while(s<i){
               cout << " ";
                s++;
                }
       j = n; 
         while(j<i)
         {
           cout << "* ";
            j--;
               }
// ending line after each row
      cout << "\n";
  i++;
}
return 0;
}

Output

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

Related Topics

Array of Object in C++

What is an Array? In C++, an array is a group of related data types, such as int, char, float, double, etc., that are easily accessed by index value alone and...

12 minutes read.

Hello World Program in C++

The steps for “Hello World” C++ program are as follows: Write a C++ code given below in an editor. Save the file with .cpp Compile the code using C++ compiler or using online...

3 minutes read.

Nullptr in C++

What is Nullptr in C++? A null pointer value is represented by the term nullptr. Use a null pointer value to indicate that a native pointer type, inner pointer, or object...

3 minutes read.

C++ Variable

In this article, we will discuss variables in C++ with their types and examples. What are Variables? Variables are specific memory storage spaces that hold a value. During the execution of a...

4 minutes read.

Inheritance in C++ vs Java

Just like we inherit traits from our parents, object-oriented programming has a concept called inheritance. In terms of object-oriented programming, a class's traits and behaviours, or its data and methods,...

4 minutes read.

Divide by Zero Exception in C++

We use exception handling method to handle the divide by zero exception. Dividing a number with zero is generally mathematical error. We have to exception handling method to overcome this...

2 minutes read.

Exception Handling in C++ vs Java

Nowadays, exception handling is a feature found in virtually all object-oriented languages. We can also find this type of feature in Java and C++. The try-catch and block is required...

3 minutes read.

How to create a directory or folder in C/C++?

A directory is to lists all files and subdirectories in a directory, set of the files will be kept in the directory, which is a location. A subdirectory is a...

5 minutes read.

C++ array to function

Arrays in C++ : Instead of defining distinct variables for each item, arrays are used to hold numerous values in a single variable. An array can be declared by specifying the variable...

4 minutes read.

Size_t Data Type in C++

In C++, the type to express the object size in bytes is defined as Size_t, an unsigned integer type offered by the standard library for describing the object's size and...

3 minutes read.

C++ Mutable keyword

C++ mutable keyword Mutable class storage specifier in C++ Storage class specifiers in C are auto, register, static, and external. Typedef is also considered as a specifier of the storage...

4 minutes read.

Destructor

Let’s discover the C++ destructor, virtual and pure virtual destructors, and when destructors are invoked and their rules in this lesson. Destructors : A member function that destroys an object is known as...

5 minutes read.

Copy constructor

Let's start by learning what a constructor is before diving into the copy constructor in C++. What is a constructor? A constructor is a particular type of class member function that configures the objects of...

7 minutes read.

Returning a Function Pointer from a Function in C/C++

Pointers to functions can be used in the C programming language just like standard data pointers such as "int *," "char *," etc. The following is a basic example of a...

3 minutes read.

Ascending order in C++

In C++, the term "ascending order" refers to a specific order in which a list of elements is arranged. When a list of elements is arranged in ascending order, the...

3 minutes read.

Functions in C++ with Types and Examples

A function is a collection of statements that work together to complete a certain goal. It could consist of statements that execute repetitive operations or statements that conduct specialized jobs...

10 minutes read.

Bit Manipulation in C++

The high-level language in which we communicate is not understood by the computer. As a result, there existed a standard mechanism for understanding any instruction sent to the computer. At...

5 minutes read.

Types of polymorphism in C++

What is polymorphism in C++ ? Polymorphism literally translates to "multiple forms". This indicates that the same thing behaves differently depending on the context in programming. Polymorphism is a feature of C++...

6 minutes read.

Add two numbers represented by two arrays in C++

The array stores a number in such a way that each digit of the number is represented by an array element. As an example, The array number 147 is 1,4,7. To add...

3 minutes read.

Use of Inheritance in C++

What is inheritance in c++? Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the...

4 minutes read.