×

Diamond Pattern Using Do-While loop in C++

What is Do-While Loop?

An iterative loop that checks the condition at the end.
The Do-While loop can be used whenever a test condition is specific, as the control enters the loop at a minimum once before the condition is evaluated. This loop checks the given condition after the execution of looping statements.

The loop actions or statements will be repeated infinitely as long as the test requirements are met.

Note: DO-WHILE LOOP means “first do it, then check”.

Syntax:

Do
           { 
            statement(s);
           } 
         while(expression);

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.
    • The outer do-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.
    • 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;
                           // outer loop(to print rows)
       do {
           j=1;
                                    //inner loop(to print columns)
                  do{
                       cout << "* ";
                  j++;
                  } while(j<=i)     
                                     //Ending line after each row //
           cout << "\n";
            i++;
          } while( i<=n )                
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 do-while loops.
    • The outer do-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.
    • The inner do-while 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;
      do{
j=1;
                   do  {
                         cout << " * ";
                   j++;
                    } While ( j< =1 )
// ending line after each row
      cout <<  " \n ";
            i++;
        } while ( i> =1 )
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 do-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)
           do{
             //while loop for displaying space
            s=1;
              do {
                     cout << " ";
                     s++;
                     } while(s<n)
              //while loop to display star equal to the row number
            j=1;
           do{
                cout << "*";
                 j++;
                } while(j<=(2*i-1))
// ending line after each row
cout << "\n";
i++;
} while(i<=n)
}

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

Output:

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

Related Topics

C++ Call by Value

In this article, we will discuss C++ Call by Value with their syntax, examples, use cases, advantages, and disadvantages. C++ Function A function is a set of statements that executes a task....

5 minutes read.

sort() function in C++

This tutorial covers the various built-in sort functions found in the C++ algorithm’s library.  What Does C++ Sort Mean? The concept of sorting in C++ entails rearranging an array's elements in a...

3 minutes read.

How to concatenate two strings in C++

In the C++ programming language, the concatenation of two or even more strings is covered in this section. The term "string concatenation" refers to a collection of characters that join two...

4 minutes read.

Passing a Vector to a function in C++

A pointer is sent to the function when we feed it an array. However, there are two ways to pass a vector: Pass by Value Pass by Reference A copy of a vector...

3 minutes read.

C++ Fibonacci Series

What is a Fibonacci series? A Fibonacci series or sequence is a very popular programming paradigm. The next element occurring in the N terms series is determined by the sum of...

2 minutes read.

Passing by Reference Vs. Passing by the pointer in C++

 Passing by Reference Vs. Passing by the pointer in C++ Throughout C++, it can transfer parameter values except by pointers or through referring to a function. For both cases, we have...

3 minutes read.

Delete Operator in C++

Overview We can reserve allocation for a variable or an array at runtime in C++. Dynamic memory allocation is the term for this. After utilising a variable in C++, we must...

4 minutes read.

List back () function in C++ STL

The list::back () function of the C++ STL returns a direct reference to the last element in the list container. This function varies from list::end (), which just returns an...

2 minutes read.

Constructor Overloading

The program contains more than one constructor in a class with the same name, and different types of arguments are called constructor overloading. Calling of constructor depends on the number and types...

2 minutes read.

C++ String

A string is a collection of characters. C++ programming language supports both C string as well as standard C++ library string. In C++, string is an object of std::string class. C Style String The C style...

5 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.

How to enter a name in C++

A name is a string or array of characters or letters. The string is one of the most helpful data types offered by the C++ library. A string helps the...

4 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.

C++ String Concatenation

C++ String Concatenation In this section, we will learn about C ++ String Concatenation, what it does, how it works, and will also see its programs. What is the String Concatenation? The + operator...

3 minutes read.

How to make a password program in C++

Before understanding the password program, one must know about a password and why it is required. Password: A password is a word that permits access to somewhere or something. A password...

4 minutes read.

Vector resize() in C++

Vector resize() in C++ Vectors are called dynamic arrays and can automatically adjust their size when a component is added or deleted. This container is used for storage. The function modifies the...

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++ Infinite loop

The term "infinite loop" refers to a loop that does not terminate the loop according to the condition. In some cases, an infinite loop may be required in programming, or...

4 minutes read.

C++ Encapsulation

The following two essential components are present in all C++ programmes: Functions are the parts of a programme that perform actions, and they are termed programme statements (code).Program data is the...

4 minutes read.

Hexadecimal to Decimal in C++

In computers, hexadecimal numbers are represented with base 16 and decimal numbers are represented with base 10 and values 0-9, whereas hexadecimal numbers have digits ranging from 0 to 15,...

3 minutes read.