×

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++ Overriding

C++ Function Overriding When the base and derive class both contain the same function name and calling the function through derived object invokes derived class function called function overriding. Function overloading is...

1 minute read.

Single Handling in C++

Introduction: Single handling in C++ refers to a technique for processing multiple events or requests with a single function or handler rather than creating separate functions for each task. This allows...

5 minutes read.

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

Splitting a string in C++

Any programming language must have the ability to work with string data. For programming needs, we sometimes need to separate string data. Many computer languages provide a split() method that...

4 minutes read.

Abstract class in C++

In this article, you will get exposure to an abstract class in C++. We will discuss this topic using some practical examples too. To understand the abstract classes, you should...

6 minutes read.

C++ Object Class

C++ Object Class Overview: C++ is a high-level programming language and an object-oriented programming language. An object-oriented language always has some properties of classes and objects. In this article, we...

4 minutes read.

C++ OOPs Concept

The main goal of C ++ programming is to add the idea of ​​object orientation to the C programming language. Inheritance, data binding, polymorphism and other concepts are part of...

4 minutes read.

Difference between Two Sets in C++

The distinction between the two sets is made up of the components that are present in the first set but absent from the second set. The function consistently duplicates the...

3 minutes read.

Different Ways to Compare Strings in C++

This section will go over the many methods for comparing strings in the C++ programming language. The string comparison checks if the first string is equal to another string. HELLO...

6 minutes 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++ operator

In this article, we will discuss about the operators in C++ with their types and examples. An operator is specially a symbol that tells compiler to perform specific manipulation. C++ contains...

5 minutes read.

Array program in C++

What is an Array? An array is a set of identically typed elements that are organized into contiguous memory locations and each element can be independently accessed using an index. We can...

16 minutes read.

Copy elision in C++

The Copy Omission is another name for the Copy Elision. One of the several compiler optimization techniques is copy elision. It prevents items from being copied inadvertently. This Copy Elision approach...

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

Dynamic Binding in C++

The notion of dynamic binding solved the challenges associated with static binding. Static binding refers to bindings that can be resolved by the compiler at runtime. All storage, stationary, and...

3 minutes read.

Palindrome Using While Loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

C++ Writing to file

In file handling, write() function is used to write data into the file. The write() uses ofstream or fstream library to write into the file. Syntax file-stream-class   file-stream-object;   file-stream-object.write((char *)&var , sizeof (var)); The write() takes two arguments. The first argument is the address of variable var...

2 minutes read.

Sum of all elements between k1’th and k2’th Smallest Elements

In this tutorial, we will look at how to determine sum of all given elements between two given indexes’ smallest elements. Assuming an array of integers and two numbers, k1...

2 minutes read.

Advantage and disadvantage friend function C++

Friend Function: - A friend function in C++ is a function that can access the private, protected, and public members of a class. In C++, a friend function is a function that...

2 minutes read.

C++ Installation

Let's install C++ setup to start programming in C++. C++ setup contains C++ compiler which is required in your system. There are lots of C++ compilers available, you must choose...

1 minute read.