Program to Print X Star Pattern in C, C++, and Python

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

Examples:

Example1:

Input:

Given number of rows =10

Output:

*                 * 
  *             *   
    *         *     
      *     *       
        * *         
        * *         
      *     *       
    *         *     
  *             *   
*                 *
Example2:
Input:
Given number of rows =10
Given character to print='@'
Output:
Enter some random number of rows = 10
Enter some random character = @
@                 @ 
  @             @   
    @         @     
      @     @       
        @ @         
        @ @         
      @     @       
    @         @     
  @             @   
@                 @

Program to Print X Star Pattern in C, C++, and Python

Method #1: Using For Loop (Star Character)

Approach:
  • Give the number of rows of the x pattern as static input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Loop from 0 to the number of rows using another For loop(Inner For loop).
  • 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.
  • Print the star character with space if the condition is true.
  • Else print space character.
  • Print the newline character after the end of the inner For loop.
  • The Exit of the program.
1) Python Implementation
Below is the implementation:
# 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()

Output:

*                 * 
  *             *   
    *         *     
      *     *       
        * *         
        * *         
      *     *       
    *         *     
  *             *   
*                 *
2) C++ Implementation
Below is the implementation:
#include <iostream>
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 < xrows; m++) {
        // Loop from 0 to the number of rows using another
        // For loop(Inner For loop).
        for (int n = 0; n < 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 << "* ";
            }
            else {
                // Else print space character.
                cout << "  ";
            }
        }
        // Print the newline character after the end of
        // the inner For loop.
        cout << endl;
    }

    return 0;
}
Output:
*                 * 
  *             *   
    *         *     
      *     *       
        * *         
        * *         
      *     *       
    *         *     
  *             *   
*                 *
3) C Implementation
Below is the implementation:
#include <stdio.h>

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 < xrows; m++) {
        // Loop from 0 to the number of rows using another
        // For loop(Inner For loop).
        for (int n = 0; n < 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;
}
Output:
*                 * 
  *             *   
    *         *     
      *     *       
        * *         
        * *         
      *     *       
    *         *     
  *             *   
*                 *

Method #2: Using For Loop (User Character)

Approach:
  • Give the number of rows of the x pattern as user input and store it in a variable.
  • Give the character to print as user input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Loop from 0 to the number of rows using another For loop(Inner For loop).
  • 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.
  • Print the given character with space if the condition is true.
  • Else print space character.
  • Print the newline character after the end of the inner For loop.
  • The Exit of the program.
1) Python Implementation
  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Give the Character as user input using input() and store it in another variable.
Below is the implementation:
# 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()

Output:

Enter some random number of rows = 10
Enter some random character = @
@                 @ 
  @             @   
    @         @     
      @     @       
        @ @         
        @ @         
      @     @       
    @         @     
  @             @   
@                 @

2) C++ Implementation

  • Give the number of rows as user input using cin and store it in a variable.
  • Give the Character as user input using cin and store it in another variable.

Below is the implementation:

#include <iostream>
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 << "Enter some random number of rows = " << endl;
    cin >> xrows;
    // Give the Character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character = " << endl;
    cin >> givencharacter;
    cout << endl;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < xrows; m++) {
        // Loop from 0 to the number of rows using another
        // For loop(Inner For loop).
        for (int n = 0; n < 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 << givencharacter << " ";
            }
            else {
                // Else print space character.
                cout << "  ";
            }
        }
        // Print the newline character after the end of
        // the inner For loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows = 
10
Enter some random character = 
@
@                 @ 
  @             @   
    @         @     
      @     @       
        @ @         
        @ @         
      @     @       
    @         @     
  @             @   
@                 @

3) C Implementation

  • Give the number of rows as user input using scanf and store it in a variable.
  • Give the Character as user input using scanf and store it in another variable.

Below is the implementation:

#include <stdio.h>

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", &xrows);
    scanf("%c", &givencharacter);
    printf("\n");

    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < xrows; m++) {
        // Loop from 0 to the number of rows using another
        // For loop(Inner For loop).
        for (int n = 0; n < 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;
}

Output:

10@
@                 @ 
  @             @   
    @         @     
      @     @       
        @ @         
        @ @         
      @     @       
    @         @     
  @             @   
@                 @

 

19