C++ Program to Print X Star Pattern


Displaying star patterns in different shapes, like pyramids, squares, and diamonds, is a common part of basic programming and logic development. We faced various problems involving stars and numerical patterns as we studied looping statements in programming. This article will demonstrate how to print an X or a cross using stars.

We will see two methods for the same. First one is little bit complicated but the next method is much efficient.

X Star Pattern (Using two sets of blank spaces)

*       *
 *     *
  *   *
   * *
    *
   * *
  *   *
 *     *
*       *

For this pattern, the line count is n = 5. This is for upper half. The total X pattern has 2n – 1 lines. Let us see how we can achieve this using the following table −

Line Number Star Count Space Left Space Between Description
1 2 0 7 When the i = n, print one star, otherwise 2. The left spaces are (i – 1) and between spaces are 2(n – i) - 1
2 2 1 5
3 2 2 3
4 2 3 1
5 1 4 -
6 2 3 1 Left stars are decreasing like n – (i – n) – 1 = 2n – i – 1. Space between will follow: 2*(i – n) - 1
7 2 2 3
8 2 1 5
9 2 0 7

Algorithm

  • read n as input
  • for i ranging from 1 to 2n - i, do
    • if i <= n, then
      • for j ranging from 1 to i - 1, do
        • display blank space
      • end for
      • display star
      • if i and n are not the same, then
        • for j ranging from 1 to 2(n - i) - 1, do
          • display blank space
        • end for
        • display star
      • end if
    • otherwise
      • for j ranging from 1 to 2n - i - 1, do
        • display blank space
      • end for
      • display star
      • for j ranging from 1 to 2(i - n) - 1, do
        • display blank space
      • end for
      • display star
    • end if
    • move the cursor to the next line
  • end for

Example

#include <iostream>
using namespace std;
void solve( int n ){
   for ( int i = 1; i <= 2*n - 1; i++ ) {
      if ( i <= n ) {
         for ( int j = 1; j <= i - 1; j++ ) {
            cout << ". ";
         }
         cout << "*  ";
         if ( i != n ) {
            for ( int j = 1; j <= 2 * (n - i) - 1; j++ ) {
               cout << "  ";
            }
            cout << "*  ";
         }
      } else {
         for ( int j = 1; j <= (2 * n) - i - 1; j++ ) {
            cout << ". ";
         }
         cout << "*  ";
         for ( int j = 1; j <= 2 * (i - n) - 1; j++ ) {
            cout << "  ";
         }
         cout << "*  ";
      }
      cout << "\n";
   }
}
int main(){
   int n = 8;
   cout << "X Star Pattern for " << n << " lines." << endl;
   solve( n );
}

Output

X Star Pattern for 8 lines.
*                            *  
. *                        *  
. . *                    *  
. . . *                *  
. . . . *            *  
. . . . . *        *  
. . . . . . *    *  
. . . . . . . *  
. . . . . . *    *  
. . . . . *        *  
. . . . *            *  
. . . *                *  
. . *                    *  
. *                        *  
*                            *

Output (With n = 10)

X Star Pattern for 10 lines.
*                                    *  
. *                                *  
. . *                            *  
. . . *                        *  
. . . . *                    *  
. . . . . *                *  
. . . . . . *            *  
. . . . . . . *        *  
. . . . . . . . *    *  
. . . . . . . . . *  
. . . . . . . . *    *  
. . . . . . . *        *  
. . . . . . *            *  
. . . . . *                *  
. . . . *                    *  
. . . *                        *  
. . *                            *  
. *                                *  
*                                    *  

Using Grid Method

The same problem can be solved by considering a grid, and from this grid, we can calculate the formula for which the stars are being printed and where the spaces are printed.

* *
* *
* *
* *
*
* *
* *
* *
* *

From the above grid, it is easy to understand, the stars will only be placed when column numbers are same as row numbers (diagonally) and column numbers are (2n + 1 – i)

Algorithm

  • read n as input
  • m = 2n - i
  • for i ranging from 1 to m, do
    • for j ranging from 1 to m, do
      • if j is same as i or j is same as (m + 1) - i, do
        • display star
      • otherwise
        • display space
      • end if
    • end for
    • move the cursor to the next line
  • end for

Example

#include <iostream>
using namespace std;
void solve( int n ){
   int m = 2*n - 1;
   for ( int i = 1; i <= m; i++ ) {
      for ( int j = 1; j <= m; j++ ) {
         if (j == i || j == (m + 1 - i))
            cout << "* ";
         else
            cout << ". ";
      }
      cout << endl;
   }
}
int main(){
   int n = 6;
   cout << "X Star Pattern for " << n << " lines." << endl;
   solve( n );
}

Output

X Star Pattern for 6 lines.
* . . . . . . . . . * 
. * . . . . . . . * . 
. . * . . . . . * . . 
. . . * . . . * . . . 
. . . . * . * . . . . 
. . . . . * . . . . . 
. . . . * . * . . . . 
. . . * . . . * . . . 
. . * . . . . . * . . 
. * . . . . . . . * . 
* . . . . . . . . . * 

Output (With n = 8)

X Star Pattern for 8 lines.
* . . . . . . . . . . . . . * 
. * . . . . . . . . . . . * . 
. . * . . . . . . . . . * . . 
. . . * . . . . . . . * . . . 
. . . . * . . . . . * . . . . 
. . . . . * . . . * . . . . . 
. . . . . . * . * . . . . . . 
. . . . . . . * . . . . . . . 
. . . . . . * . * . . . . . . 
. . . . . * . . . * . . . . . 
. . . . * . . . . . * . . . . 
. . . * . . . . . . . * . . . 
. . * . . . . . . . . . * . . 
. * . . . . . . . . . . . * . 
* . . . . . . . . . . . . . * 

Conclusion

Star patterns are simple to use and useful for learning programming looping ideas. This article demonstrated how to use C++ to display half-diamond patterns for both left- and right-aligned half-diamonds. The X or Cross pattern is then displayed using stars after taking into account the n-line count. For the same, we have provided two methods. One employ padding and white space, while the other makes use of grid computations. Instead of adding spaces, we've added dots. If not, they occasionally trim blank spaces from the output.

Updated on: 14-Dec-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements