×

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

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.

Malloc() and new in C++

In C ++, malloc () and new are used for the same thing. During runtime, they are used to allocate memory. Malloc () and the new, on the other hand,...

4 minutes read.

C++ Pipe Tutorial

A pipe is a mechanism for inter-process communication (IPC) in a Unix-like operating system. It allows two or more processes to communicate with each other by sending and receiving data...

3 minutes read.

How to Declare Unordered Sets in C++

The implementation of an unordered set using a hash table ensures that the insertion is always randomised by hashing the keys into hash table indices. When we define keys of...

4 minutes read.

C++ Inline function

A C++ function that extends in line when called is known as an inline function. It reduces function call overhead by having the compiler use the function code rather than...

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.

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.

Name Mangling and extern in C++

Name Mangling and Function Overloading: Function overloading is a feature offered by C++. As long as each function accepts various parameters, we can use this to write many functions with the...

4 minutes read.

wcscpy(), wcslen(), wcscmp() Functions in C++

There are many built-in functions in C++ programming language which differentiate it from C programming language in most hardware-coded languages. We will now closely look into the applications of three...

4 minutes read.

Const_cast in C++ Type Casting Operators

In this tutorial, we will learn enough about const cast in the most widely used programming language, C++, as well as type casting operations. C++ supports the four types of casting...

4 minutes read.

C++ Scope of Variables

In this tutorial, we will explore about the scope of variables in c++ programming language. And also, how it works in a program. What is Scope? The range of applications for something...

4 minutes read.

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.

Program to arrange an array in alternate positive and negative numbers

Let’s say, there is given an array arr, arrange the array in such a way that every positive number is followed by a negative number. If there are extra positive...

4 minutes read.

C++ File Handling

File handling is a mechanism that manipulates the data stored in files. File handling store output data from the program to external file and read file data to the program. There...

3 minutes read.

Factorial Program in C++

C++ Factorial Program: The product of all positive descending integers is the factorial of n. n! denotes the factorial of n. For instance: 5! = 5*4*3*2*1=120 4! = 4*3*2*1=24 In Combinations and Permutations, the...

4 minutes read.

Pointers in C++

Pointers are a powerful feature in the C++ programming language, allowing developers to directly manipulate memory addresses and create more efficient and dynamic programs. However, pointers can also source various...

3 minutes read.

C++ Virtual Destructor

In C++, a destructor is a class member function that is used to free up space or remove an object of the class that has gone out of scope. The...

4 minutes read.

Dynamic Constructor in C++

Dynamic constructors create dynamic memory by using a dynamic memory allocator new within the constructor. This allows us to initialize objects dynamically. The term dynamic constructor is used when memory...

2 minutes read.

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.

C++ Exception Handling

Exception is an unexpected problem that occurs at program run time. This problem might include condition such as division by zero, running out of memory space, array out of bonds, etc....

2 minutes read.