Program to Print X Star Pattern in C, C++, and Python
Tags: python, programming, cpp
Published: 2021-07-08 12:34:52

Give the rows of the pattern the task is to print X star Pattern in C, C++, and Python.

<strong>Examples:</strong>

<strong>Example1:</strong>

<strong>Input:</strong>
<pre>Given number of rows =10</pre>
<strong>Output:</strong>
<pre id="codeOutputPre">*                 * 
  *             *   
    *         *     
      *     *       
        * *         
        * *         
      *     *       
    *         *     
  *             *   
*                 *</pre>
<strong>Example2:</strong>

<strong>Input:</strong>
<pre>Given number of rows =10
Given character to print='@'</pre>
<strong>Output:</strong>
<pre id="codeOutputPre">Enter some random number of rows = 10
Enter some random character = @
@                 @ 
  @             @   
    @         @     
      @     @       
        @ @         
        @ @         
      @     @       
    @         @     
  @             @   
@                 @</pre>
<h2>Program to Print X Star Pattern in C, C++, and Python</h2>
Below are the ways to print the X Star Pattern in C, C++, and Python.
<ul>
 	<li><a href="#Using_For_Loop_(Star_Character)">Using For Loop (Star Character)</a></li>
 	<li><a href="#Using_For_Loop_(User_Character)">Using For Loop (User Character)</a></li>
</ul>
<h3 id="Using_For_Loop_(Star_Character)">Method #1: Using For Loop (Star Character)</h3>
<strong>Approach:</strong>
<ul>
 	<li>Give the number of rows of the x pattern as static input and store it in a variable.</li>
 	<li>Loop from 0 to the number of rows using For loop.</li>
 	<li>Loop from 0 to the number of rows using another For loop(Inner For loop).</li>
 	<li>Check if the parent loop iterator value is equal to the inner loop iterator value or if the inner loop iterator value is equal to the number of rows-parent loop iterator value-1 using If conditional Statement.</li>
 	<li>Print the star character with space if the condition is true.</li>
 	<li>Else print space character.</li>
 	<li>Print the newline character after the end of the inner For loop.</li>
 	<li>The Exit of the program.</li>
</ul>
<strong>1) Python Implementation</strong>

<strong>Below is the implementation:</strong>
<pre class="EnlighterJSRAW" data-enlighter-language="python"># Give the number of rows of the x pattern as static input and store it in a variable.
xrows=10

#Loop from 0 to the number of rows using For loop.
for m in range(0, xrows):
    # Loop from 0 to the number of rows using another For loop(Inner For loop).
    for n in range(0, xrows):
        '''Check if the parent loop iterator value is equal to the inner loop 
        iterator value or if the inner loop iterator value is equal to the number
        of rows-parent loop iterator value-1 using If conditional Statement.'''
        if(m==n or n==xrows - 1 - m):
          #Print the star character with space if the condition is true.
          print('*',end=' ')
        else:
          #Else print space character.
          print(' ',end=' ')
     #Print the newline character after the end of the inner For loop.      
    print()
</pre>
<strong>Output:</strong>
<pre id="codeOutputPre">*                 * 
  *             *   
    *         *     
      *     *       
        * *         
        * *         
      *     *       
    *         *     
  *             *   
*                 *</pre>
<li><a href="https://btechgeeks.com/python-program-to-print-plus-star-pattern/">Python Program to Print Plus Star Pattern</a></li>
&nbsp;
<strong>2) C++ Implementation</strong>

<strong>Below is the implementation:</strong>
<pre class="EnlighterJSRAW" data-enlighter-language="cpp">#include &lt;iostream&gt;
using namespace std;

int main()
{
    // Give the number of rows of the x pattern as static
    // input and store it in a variable.
    int xrows = 10;

    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m &lt; xrows; m++) {
        // Loop from 0 to the number of rows using another
        // For loop(Inner For loop).
        for (int n = 0; n &lt; xrows; n++) {
            /*Check if the parent loop iterator value is
            equal to the inner loop iterator value or if the
            inner loop iterator value is equal to the
            number of rows-parent loop iterator value-1
            using If conditional Statement.*/
            if (n == m or n == (xrows - m - 1)) {
                // Print the star character with space if
                // the condition is true.
                cout &lt;&lt; "* ";
            }
            else {
                // Else print space character.
                cout &lt;&lt; "  ";
            }
        }
        // Print the newline character after the end of
        // the inner For loop.
        cout &lt;&lt; endl;
    }

    return 0;
}</pre>
<strong>Output:</strong>
<pre id="codeOutputPre">*                 * 
  *             *   
    *         *     
      *     *       
        * *         
        * *         
      *     *       
    *         *     
  *             *   
*                 *</pre>
<strong>3) C Implementation</strong>

<strong>Below is the implementation:</strong>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include &lt;stdio.h&gt;

int main()
{
  // Give the number of rows of the x pattern as static
    // input and store it in a variable.
    int xrows = 10;

    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m &lt; xrows; m++) {
        // Loop from 0 to the number of rows using another
        // For loop(Inner For loop).
        for (int n = 0; n &lt; xrows; n++) {
            /*Check if the parent loop iterator value is
            equal to the inner loop iterator value or if the
            inner loop iterator value is equal to the
            number of rows-parent loop iterator value-1
            using If conditional Statement.*/
            if (n == m || n == (xrows - m - 1)) {
                // Print the star character with space if
                // the condition is true.
                printf("* ");
            }
            else {
                // Else print space character.
                printf("  ");
            }
        }
        // Print the newline character after the end of
        // the inner For loop.
        printf("\n");
    }

    return 0;
}</pre>
<strong>Output:</strong>
<pre id="codeOutputPre">*                 * 
  *             *   
    *         *     
      *     *       
        * *         
        * *         
      *     *       
    *         *     
  *             *   
*                 *</pre>
<h3 id="Using_For_Loop_(User_Character)">Method #2: Using For Loop (User Character)</h3>
<strong>Approach:</strong>
<ul>
 	<li>Give the number of rows of the x pattern as user input and store it in a variable.</li>
 	<li>Give the character to print as user input and store it in a variable.</li>
 	<li>Loop from 0 to the number of rows using For loop.</li>
 	<li>Loop from 0 to the number of rows using another For loop(Inner For loop).</li>
 	<li>Check if the parent loop iterator value is equal to the inner loop iterator value or if the inner loop iterator value is equal to the number of rows-parent loop iterator value-1 using If conditional Statement.</li>
 	<li>Print the given character with space if the condition is true.</li>
 	<li>Else print space character.</li>
 	<li>Print the newline character after the end of the inner For loop.</li>
 	<li>The Exit of the program.</li>
</ul>
<strong>1) Python Implementation</strong>
<ul>
 	<li>Give the number of rows as user input using int(input()) and store it in a variable.</li>
 	<li>Give the Character as user input using input() and store it in another variable.</li>
</ul>
<strong>Below is the implementation:</strong>
<pre class="EnlighterJSRAW" data-enlighter-language="python"># Give the number of rows  as user input using int(input()) and store it in a variable.
xrows = int(input(
    'Enter some random number of rows  = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Loop from 0 to the number of rows using For loop.
for m in range(0, xrows):
    # Loop from 0 to the number of rows using another For loop(Inner For loop).
    for n in range(0, xrows):
        '''Check if the parent loop iterator value is equal to the inner loop 
        iterator value or if the inner loop iterator value is equal to the number
        of rows-parent loop iterator value-1 using If conditional Statement.'''
        if(m == n or n == xrows - 1 - m):
            # Print the given character with space if the condition is true.
            print(givencharacter, end=' ')
        else:
            # Else print space character.
            print(' ', end=' ')
       # Print the newline character after the end of the inner For loop.
    print()
</pre>
<strong>Output:</strong>
<pre id="codeOutputPre">Enter some random number of rows = 10
Enter some random character = @
@                 @ 
  @             @   
    @         @     
      @     @       
        @ @         
        @ @         
      @     @       
    @         @     
  @             @   
@                 @</pre>
<strong>2) C++ Implementation</strong>
<ul>
 	<li>Give the number of rows as user input using cin and store it in a variable.</li>
 	<li>Give the Character as user input using cin and store it in another variable.</li>
</ul>
<strong>Below is the implementation:</strong>
<pre class="EnlighterJSRAW" data-enlighter-language="cpp">#include &lt;iostream&gt;
using namespace std;

int main()
{
    // Give the number of rows
    // as user input using cin and store it in a
    // variable.
    int xrows;
    char givencharacter;
    cout &lt;&lt; "Enter some random number of rows = " &lt;&lt; endl;
    cin &gt;&gt; xrows;
    // Give the Character as user input using cin and store
    // it in another variable.
    cout &lt;&lt; "Enter some random character = " &lt;&lt; endl;
    cin &gt;&gt; givencharacter;
    cout &lt;&lt; endl;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m &lt; xrows; m++) {
        // Loop from 0 to the number of rows using another
        // For loop(Inner For loop).
        for (int n = 0; n &lt; xrows; n++) {
            /*Check if the parent loop iterator value is
            equal to the inner loop iterator value or if the
            inner loop iterator value is equal to the
            number of rows-parent loop iterator value-1
            using If conditional Statement.*/
            if (n == m || n == (xrows - m - 1)) {
                // Print the given  character with space if
                // the condition is true.
                cout &lt;&lt; givencharacter &lt;&lt; " ";
            }
            else {
                // Else print space character.
                cout &lt;&lt; "  ";
            }
        }
        // Print the newline character after the end of
        // the inner For loop.
        cout &lt;&lt; endl;
    }

    return 0;
}</pre>
<strong>Output:</strong>
<pre id="codeOutputPre">Enter some random number of rows = 
10
Enter some random character = 
@
@                 @ 
  @             @   
    @         @     
      @     @       
        @ @         
        @ @         
      @     @       
    @         @     
  @             @   
@                 @</pre>
<strong>3) C Implementation</strong>
<ul>
 	<li>Give the number of rows as user input using scanf and store it in a variable.</li>
 	<li>Give the Character as user input using scanf and store it in another variable.</li>
</ul>
<strong>Below is the implementation:</strong>
<pre class="EnlighterJSRAW" data-enlighter-language="c">#include &lt;stdio.h&gt;

int main()
{

    // Give the number of rows
    //  as user input using scanf and store it in a
    // variable.
    int xrows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &amp;xrows);
    scanf("%c", &amp;givencharacter);
    printf("\n");

    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m &lt; xrows; m++) {
        // Loop from 0 to the number of rows using another
        // For loop(Inner For loop).
        for (int n = 0; n &lt; xrows; n++) {
            /*Check if the parent loop iterator value is
            equal to the inner loop iterator value or if the
            inner loop iterator value is equal to the
            number of rows-parent loop iterator value-1
            using If conditional Statement.*/
            if (n == m || n == (xrows - m - 1)) {
                // Print the given character with space if
                // the condition is true.
                printf("%c", givencharacter);
            }
            else {
                // Else print space character.
                printf("  ");
            }
        }
        // Print the newline character after the end of
        // the inner For loop.
        printf("\n");
    }

    return 0;
}</pre>
<strong>Output:</strong>
<pre id="codeOutputPre">10@
@                 @ 
  @             @   
    @         @     
      @     @       
        @ @         
        @ @         
      @     @       
    @         @     
  @             @   
@                 @</pre>
&nbsp;