Python

Python Program to Print the Equilateral Triangle Pattern of Star

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Given the number of rows, the task is to Print Equilateral triangle Pattern of Star in C, C++, and Python

Examples:

Example1:

Input:

Given number of rows = 8

Output:

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

Example2:

Input:

Given number of rows = 10
Given Character to print ='$'

Output

                  $ 
                 $ $ 
                $ $ $ 
               $ $ $ $ 
              $ $ $ $ $ 
             $ $ $ $ $ $ 
            $ $ $ $ $ $ $ 
           $ $ $ $ $ $ $ $ 
          $ $ $ $ $ $ $ $ $ 
         $ $ $ $ $ $ $ $ $ $

Program to Print the Equilateral triangle Pattern of Star in C, C++, and Python

Below are the ways to Print the Equilateral triangle Pattern of Star in C, C++, and Python.

Method #1: Using For Loop (Star Character)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Take a variable to say g and initialize its value with (2*number of rows)-2.
  • Loop from 0 to the number of rows using For loop.
  • Loop from 0 to g using another For Loop(Inner For loop).
  • Print the space character in the inner For Loop.
  • Decrement the value of g by 1 after the end of the inner For loop.
  • Loop from 0 to m+1 using another For loop(Nested For loop) where m is the iterator value of the parent For loop.
  • Print the star character with space.
  • Print the Newline character after the end of the Two inner For loops.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numberOfRows = 8
# Take a variable to say g and initialize its value with (2*number of rows)-2.
g = (2*numberOfRows)-2
# Loop from 0 to the number of rows using For loop.
for m in range(0, numberOfRows):
    # Loop from 0 to g using another For Loop(Inner For loop).
    for n in range(0, g):
      # Print the space character in the inner For Loop.
        print(end=" ")
    # Decrement the value of g by 1 after the end of the inner For loop.
    g = g-1
    # Loop from 0 to m+1 using another For loop(Nested For loop)
    # where m is the iterator value of the parent For loop.
    for n in range(0, m+1):
      # Print the star character with space.
        print('*', end=" ")
    # Print the Newline character after the end of the Two inner For loops.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows as static input and store it
    // in a variable.
    int numberOfRows = 8;

    // Take a variable to say g and initialize its value
    // with (2*number of rows)-2.
    int g = (2 * numberOfRows) - 2;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < numberOfRows; m++) {
        // Loop from 0 to g using another For Loop(Inner For
        // loop).
        for (int n = 0; n < g; n++) {
            // Print the space character in the inner For
            // Loop.
            cout << " ";
        }
        // Decrement the value of g by 1 after the end of
        // the inner For loop.
        g = g - 1;
        // Loop from 0 to m+1 using another For loop(Nested
        // For loop) where m is the iterator value of the
        // parent For loop.
        for (int n = 0; n < m + 1; n++) {
            // Print the star character with space.
            cout << "* ";
        }
        // Print the Newline character after the end of the
        // Two inner For loops.
        cout << endl;
    }
    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows as static input and store it
    // in a variable.
    int numberOfRows = 13;

    // Take a variable to say g and initialize its value
    // with (2*number of rows)-2.
    int g = (2 * numberOfRows) - 2;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < numberOfRows; m++) {
        // Loop from 0 to g using another For Loop(Inner For
        // loop).
        for (int n = 0; n < g; n++) {
            // Print the space character in the inner For
            // Loop.
            printf(" ");
        }
        // Decrement the value of g by 1 after the end of
        // the inner For loop.
        g = g - 1;
        // Loop from 0 to m+1 using another For loop(Nested
        // For loop) where m is the iterator value of the
        // parent For loop.
        for (int n = 0; n < m + 1; n++) {
            // Print the star character with space.
            printf("* ");
        }
        // Print the Newline character after the end of the
        // Two inner For loops.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For Loop (User Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Scan the character to print as user input and store it in a variable.
  • Take a variable to say g and initialize its value with (2*number of rows)-2.
  • Loop from 0 to the number of rows using For loop.
  • Loop from 0 to g using another For Loop(Inner For loop).
  • Print the space character in the inner For Loop.
  • Decrement the value of g by 1 after the end of the inner For loop.
  • Loop from 0 to m+1 using another For loop(Nested For loop) where m is the iterator value of the parent For loop.
  • Print the star character with space.
  • Print the Newline character after the end of the Two inner For loops.
  • 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.
numberOfRows = int(input('Enter some random number of rows = '))
# Give the character to print as user input using int(input()) and store it in another variable.
characte = input('Enter some random character to print = ')
# Take a variable to say g and initialize its value with (2*number of rows)-2.
g = (2*numberOfRows)-2
# Loop from 0 to the number of rows using For loop.
for m in range(0, numberOfRows):
    # Loop from 0 to g using another For Loop(Inner For loop).
    for n in range(0, g):
      # Print the space character in the inner For Loop.
        print(end=" ")
    # Decrement the value of g by 1 after the end of the inner For loop.
    g = g-1
    # Loop from 0 to m+1 using another For loop(Nested For loop)
    # where m is the iterator value of the parent For loop.
    for n in range(0, m+1):
      # Print the given character with space.
        print(characte, end=" ")
    # Print the Newline character after the end of the Two inner For loops.
    print()

Output

Enter some random number of rows = 10
Enter some random character to print = $
                  $ 
                 $ $ 
                $ $ $ 
               $ $ $ $ 
              $ $ $ $ $ 
             $ $ $ $ $ $ 
            $ $ $ $ $ $ $ 
           $ $ $ $ $ $ $ $ 
          $ $ $ $ $ $ $ $ $ 
         $ $ $ $ $ $ $ $ $ $

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 numberOfRows;
    cin >> numberOfRows;
    // Create a character variable.
    char characte;
    // Give the character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character to print = "
         << endl;
    cin >> characte;
    cout << endl;
    int g = (2 * numberOfRows) - 2;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < numberOfRows; m++) {
        // Loop from 0 to g using another For Loop(Inner For
        // loop).
        for (int n = 0; n < g; n++) {
            // Print the space character in the inner For
            // Loop.
            cout << " ";
        }
        // Decrement the value of g by 1 after the end of
        // the inner For loop.
        g = g - 1;
        // Loop from 0 to m+1 using another For loop(Nested
        // For loop) where m is the iterator value of the
        // parent For loop.
        for (int n = 0; n < m + 1; n++) {
            // Print the given character with space.
            cout << characte << " ";
        }
        // Print the Newline character after the end of the
        // Two inner For loops.
        cout << endl;
    }
    return 0;
}

Output

10
Enter some random character to print = $
                  $ 
                 $ $ 
                $ $ $ 
               $ $ $ $ 
              $ $ $ $ $ 
             $ $ $ $ $ $ 
            $ $ $ $ $ $ $ 
           $ $ $ $ $ $ $ $ 
          $ $ $ $ $ $ $ $ $ 
         $ $ $ $ $ $ $ $ $ $

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 numberOfRows;
    scanf("%d", &numberOfRows);
    // Create a character variable.
    // Give the character as user input using scanf and
    // store it in another variable.
    char characte;
    scanf("%c", &characte);
    printf("\n");
    // Take a variable to say g and initialize its value
    // with (2*number of rows)-2.
    int g = (2 * numberOfRows) - 2;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < numberOfRows; m++) {
        // Loop from 0 to g using another For Loop(Inner For
        // loop).
        for (int n = 0; n < g; n++) {
            // Print the space character in the inner For
            // Loop.
            printf(" ");
        }
        // Decrement the value of g by 1 after the end of
        // the inner For loop.
        g = g - 1;
        // Loop from 0 to m+1 using another For loop(Nested
        // For loop) where m is the iterator value of the
        // parent For loop.
        for (int n = 0; n < m + 1; n++) {
            // Print the given character with space.
            printf("%c ",characte);
        }
        // Print the Newline character after the end of the
        // Two inner For loops.
        printf("\n");
    }
    return 0;
}

Output

10$
                  $ 
                 $ $ 
                $ $ $ 
               $ $ $ $ 
              $ $ $ $ $ 
             $ $ $ $ $ $ 
            $ $ $ $ $ $ $ 
           $ $ $ $ $ $ $ $ 
          $ $ $ $ $ $ $ $ $ 
         $ $ $ $ $ $ $ $ $ $

Related Programs:

Python Program to Print the Equilateral Triangle Pattern of Star Read More »

Python Program to Print Reverse Mirrored Right Triangle Star Pattern

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Given the number of rows of the right triangle, the task is to Reverse print Mirrored Right Triangle Star Pattern in C, C++, and Python.

Examples:

Example1:

Input:

given number of rows of the right triangle =8

Output:

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

Example2:

Input:

given number of rows of the right triangle =9
Given character to print ='&'

Output:

& & & & & & & & & 
    & & & & & & & & 
        & & & & & & & 
            & & & & & & 
                & & & & & 
                    & & & & 
                        & & & 
                            & & 
                                &

Program to Print Mirrored Reverse Right Triangle Star Pattern in C, C++, and Python

Below are the ways to print Reverse Mirrored Right Triangle Star Pattern in C, C++, and Python.

Method #1: Using For loop (Star Character)

Approach:

  • Give the number of rows of the right triangle pattern as static input and store it in a variable.
  • Iterate from 1 to given rows using the First For loop.
  • Iterate from 1 to given rows using another for loop(Nested For loop)
  • Check if the iterator value of the inner for loop is less than the given first iterator value using the If statement.
  • If the statement is true then print space.
  • Else print the star character with space.
  • Print the newline character after the exit of the inner for loop.
  • The Exit of the Program.

1) Python Implementation:

Below is the implementation:

# Give the number of rows of the right triangle pattern as static input and store it in a variable.
triNumRows = 8
# Iterate from 1 to given rows using First for loop.
for m in range(1, triNumRows+1):
    # Iterate from 1 to given rows using another for loop(Nested For loop)
    for n in range(1, triNumRows+1):
        # Check if the iterator value of the inner for loop is less than the given first iterator value using the If statement.
        if(n < m):
            # If the statement is true then print space.
            print(' ', end=' ')
        else:
            # Else print star character with space.
            print('*', end=' ')
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the right triangle pattern
    //  as static input and store it in a variable.
    int triNumRows = 8;
    // Iterate from 1 to given rows using First for loop.
    for (int m = 1; m <= triNumRows; m++) {
        // Iterate from 1 to given rows using another for
        // loop(Nested For loop)
        for (int n = 1; n <= triNumRows; n++) {
            /* Check if the iterator value of the inner for
             * loop is less than the given first iterator
             * value using the If statement.*/
            if (n < m) {
                // If the statement is true then print
                // space.
                cout << "  ";
            }
            else {
                // Else print star character with space.
                cout << "* ";
            }
        }
        // Print the newline character after the exit 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 right triangle pattern
    //  as static input and store it in a variable.
    int triNumRows = 8;
    // Iterate from 1 to given rows using First for loop.
    for (int m = 1; m <= triNumRows; m++) {
        // Iterate from 1 to given rows using another for
        // loop(Nested For loop)
        for (int n = 1; n <= triNumRows; n++) {
            /* Check if the iterator value of the inner for
             * loop is less than the given first iterator
             * value using the If statement.*/
            if (n < m) {
                // If the statement is true then print
                // space.
                printf("  ");
            }
            else {
                // Else print star character with space.
                printf("* ");
            }
        }
        // Print the newline character after the exit 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 right triangle pattern as user input and store it in a variable.
  • Give the character to print as user input and store it in a variable.
  • Iterate from 1 to given rows using the First For loop.
  • Iterate from 1 to given rows using another for loop(Nested For loop)
  • Check if the iterator value of the inner for loop is less than the given first iterator value using the If statement.
  • If the statement is true then print space.
  • Else print the star character with space.
  • Print the newline character after the exit of the inner for loop.
  • The Exit of the Program.

1) Python Implementation:

  • Give the number of rows of the Right Triangle 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 of the Right Triangle as user input using int(input()) and store it in a variable.
triNumRows = int(input(
    'Enter some random number of rows of the Right Triangle Pattern = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Iterate from 1 to given rows using First for loop.
for m in range(1, triNumRows+1):
    # Iterate from 1 to given rows using another for loop(Nested For loop)
    for n in range(1, triNumRows+1):
       # Check if the iterator value of the inner for loop is less than the given first iterator value using the If statement.
        if(n < m):
            # If the statement is true then print space.
            print(' ', end=' ')
        else:
            # Else print star character with space.
            print(givencharacter, end=' ')
    print()

Output:

Enter some random number of rows of the Right Triangle Pattern = 9
Enter some random character = &
& & & & & & & & & 
    & & & & & & & & 
        & & & & & & & 
            & & & & & & 
                & & & & & 
                    & & & & 
                        & & & 
                            & & 
                                &

2) C++ Implementation

  • Give the number of rows of the Right Triangle 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 of the Right
    // Triangle as user input using cin and store it in a
    // variable.
    int triNumRows;
    char givencharacter;
    cout << "Enter some random number of rows of the "
            "Right Triangle Pattern = "
         << endl;
    cin >> triNumRows;
    // Give the Character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character = " << endl;
    cin >> givencharacter;
    cout << endl;
    // Iterate from 1 to given rows using First for loop.
    for (int m = 1; m <= triNumRows; m++) {
        // Iterate from 1 to given rows using another for
        // loop(Nested For loop)
        for (int n = 1; n <= triNumRows; n++) {
            /* Check if the iterator value of the inner for
             * loop is less than the given first iterator
             * value using the If statement.*/
            if (n < m) {
                // If the statement is true then print
                // space.
                cout << "  ";
            }
            else {
                // Else print given character with space.
                cout << givencharacter << " ";
            }
        }
        // Print the newline character after the exit of the
        // inner for loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the Right Triangle Pattern = 
9
Enter some random character = 
&
& & & & & & & & & 
    & & & & & & & & 
        & & & & & & & 
            & & & & & & 
                & & & & & 
                    & & & & 
                        & & & 
                            & & 
                                &

3) C Implementation

  • Give the number of rows of the Right Triangle 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 of the  Right
    // Triangle as user input using scanf and store it in a
    // variable.
    int triNumRows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &triNumRows);
    scanf("%c", &givencharacter);
    printf("\n");
    // Iterate from 1 to given rows using First for loop.
    for (int m = 1; m <= triNumRows; m++) {
        // Iterate from 1 to given rows using another for
        // loop(Nested For loop)
        for (int n = 1; n <= triNumRows; n++) {
            /* Check if the iterator value of the inner for
             * loop is less than the given first iterator
             * value using the If statement.*/
            if (n < m) {
                // If the statement is true then print
                // space.
                printf("  ");
            }
            else {
                // Else print star character with space.
                printf("%c ", givencharacter);
            }
        }
        // Print the newline character after the exit of the
        // inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

9&
& & & & & & & & & 
    & & & & & & & & 
        & & & & & & & 
            & & & & & & 
                & & & & & 
                    & & & & 
                        & & & 
                            & & 
                                &

Related Programs:

Python Program to Print Reverse Mirrored Right Triangle Star Pattern Read More »

Program to Print Hollow Rectangle Star Pattern in C,C++ and Python

Python Program to Print Hollow Rectangle Star Pattern

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Given the length, breadth of the rectangle the task is to print the hollow rectangle star pattern in C, C++, and Python.

Examples:

Example1:

Input:

given length of rectangle =11
given breadth of rectangle =19

Output:

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

Example2:

Input:

given length of rectangle =20
given breadth of rectangle =5

Output:

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

 

Program to Print Hollow Rectangle Star Pattern in C, C++, and Python

Below are the ways to print the Hollow Rectangle star pattern in C, C++, and Python.

Method #1: Using For loop (Static Input)

Approach:

  • Give the length and breadth as static input and store them in two variables.
  • Loop till the length of the rectangle using For loop.
  • Loop till the breadth of the rectangle using another nested For loop.
  • We use the If Else statement to check If the length or breadth number is 0 or maximum – 1.
  • If it is true then print * else print space.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the length and breadth as static input and store them in two variables.
lengthnum = 11
breadthnum = 19
# Loop till the length of the rectangle using For loop.
for m in range(lengthnum):
    # Loop till the breadth of the rectangle using another nested For loop.
    for n in range(breadthnum):
        # We use the If Else statement to check If the length or breadth number is 0 or maximum – 1.
        # If it is true then print * else print space.
        if(m == 0 or m == lengthnum - 1 or n == 0 or n == breadthnum - 1):
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()

Output:

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

2) C++Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the length and breadth as static input and store
    // them in two variables.
    int lengthnum = 11;
    int breadthnum = 23;
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum; n++) {
            // We use the If Else statement to check If the
            // length or breadth number is 0 or maximum – 1.
            // If it is true then print * else print space.
            if (m == 0 || m == lengthnum - 1 || n == 0
                || n == breadthnum - 1)
                cout << "* ";
            else
                cout << "  ";
        }
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{
    // Give the length and breadth as static input and store
    // them in two variables.
    int lengthnum = 20;
    int breadthnum = 5;
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum; n++) {
            // We use the If Else statement to check If the
            // length or breadth number is 0 or maximum – 1.
            // If it is true then print * else print space.
            if (m == 0 || m == lengthnum - 1 || n == 0
                || n == breadthnum - 1)
                printf("* ");
            else
                printf("  ");
        }
        printf("\n");
    }
}

Output:

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

Method #2: Using For loop (User Input)

Approach:

  • Give the length and breadth as user input and store them in two variables.
  • Loop till the length of the rectangle using For loop.
  • Loop till the breadth of the rectangle using another nested For loop.
  • We use the If Else statement to check If the length or breadth number is 0 or maximum – 1.
  • If it is true then print * else print space.
  • The Exit of the Program.

1) Python Implementation

Give the length and breadth of the rectangle as user input using the int(input()) function and store them in two separate variables.

Below is the implementation:

# Give the length and breadth of the rectangle as user input using the int(input()) function
# and store them in two separate variables.
lengthnum = int(input('Enter some random length value of the rectangle = '))

breadthnum = int(input('Enter some random breadth value of the rectangle = '))
# Loop till the length of the rectangle using For loop.
for m in range(lengthnum):
    # Loop till the breadth of the rectangle using another nested For loop.
    for n in range(breadthnum):
        # We use the If Else statement to check If the length or breadth number is 0 or maximum – 1.
        # If it is true then print * else print space.
        if(m == 0 or m == lengthnum - 1 or n == 0 or n == breadthnum - 1):
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()

Output:

Enter some random length value of the rectangle = 20
Enter some random breadth value of the rectangle = 5
*  *  *  *  * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*            * 
*  *  *  * *

2) C++ Implementation

Give the length and breadth of the rectangle as user input using the cin and store them in two separate variables.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the length and breadth of the rectangle as user
    // input using the cin
    // and store them in two separate variables.
    int lengthnum, breadthnum;
    cout << "Enter some random length and breadth of "
            "rectangle separated by spaces"
         << endl;
    cin >> lengthnum >> breadthnum;
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum; n++) {
            // We use the If Else statement to check If the
            // length or breadth number is 0 or maximum – 1.
            // If it is true then print * else print space.
            if (m == 0 || m == lengthnum - 1 || n == 0
                || n == breadthnum - 1)
                cout << "* ";
            else
                cout << "  ";
        }
        cout << endl;
    }

    return 0;
}

Output:

Enter some random length and breadth of rectangle separated by spaces
11 19
* * * * * * * * * * * * * * * * * * * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
*                                            * 
* * * * * * * * * * * * * * * * * * *

3) C Implementation

Give the length and breadth of the rectangle as user input using the scanf and store them in two separate variables.

Below is the implementation:

#include <stdio.h>

int main()
{
    //    Give the length and breadth of the rectangle as
    // user input using the scanf
    // and store them in two separate variables.
    int lengthnum, breadthnum;
    printf("Enter some random length and breadth of "
           "rectangle separated by spaces \n");
    scanf("%d%d", &lengthnum, &breadthnum);
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum; n++) {
            // We use the If Else statement to check If the
            // length or breadth number is 0 or maximum – 1.
            // If it is true then print * else print space.
            if (m == 0 || m == lengthnum - 1 || n == 0
                || n == breadthnum - 1)
                printf("* ");
            else
                printf("  ");
        }
        printf("\n");
    }
}

Output:

Enter some random length and breadth of rectangle separated by spaces
11 23
* * * * * * * * * * * * * * * * * * * * * * * 
*                                                      * 
*                                                      * 
*                                                      * 
*                                                      * 
*                                                      * 
*                                                      * 
*                                                      * 
*                                                      * 
*                                                      * 
* * * * * * * * * * * * * * * * * * * * * * *

Here we printed two blank spaces if the else condition is executed.

Python Program to Print Hollow Rectangle Star Pattern Read More »

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

Python Program to Print Rectangle Star Pattern

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Given the length, breadth of the rectangle the task is to print the rectangle star pattern in C, C++, and Python.

Examples:

Example1:

Input:

given length of rectangle =8
given breadth of rectangle =29

Output:

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

Example2:

Input:

given length of rectangle =19
given breadth of rectangle =5

Output:

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

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

Below are the ways to print the rectangle star pattern in C, C++, and Python.

Method #1: Using For loop (Static Input)

Approach:

  • Give the length and breadth as static input and store them in two variables.
  • Loop till the length of the rectangle using For loop.
  • Loop till the breadth of the rectangle using another nested For loop.
  • Print the * character inside the nested For loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the length and breadth as static input and store them in two variables.
lengthnum = 8
breadthnum = 11
# Loop till the length of the rectangle using For loop.
for m in range(lengthnum):
    # Loop till the breadth of the rectangle using another nested For loop.
    for n in range(breadthnum):
        # Print the * character inside the nested For loop.
        print('* ', end=' ')
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the length and breadth as static input and store
    // them in two variables.
    int lengthnum = 11;
    int breadthnum = 23;
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum;n++) { // Print the * character inside the
                    // nested For loop.
            cout << "* ";
        }
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{
    // Give the length and breadth as static input and store
    // them in two variables.
    int lengthnum = 9;
    int breadthnum = 29;
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum; n++) {
            // Print the * character inside the
            // nested For loop.
            printf("* ");
        }
        printf("\n");
    }
}

Output:

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

Method #2: Using For loop (User Input)

Approach:

  • Give the length and breadth as user input and store them in two variables.
  • Loop till the length of the rectangle using For loop.
  • Loop till the breadth of the rectangle using another nested For loop.
  • Print the * character inside the nested For loop.
  • The Exit of the Program.

1) Python Implementation

Give the length and breadth of the rectangle as user input using the int(input()) function and store them in two separate variables.

Below is the implementation:

# Give the length and breadth of the rectangle as user input using the int(input()) function
# and store them in two separate variables.
lengthnum = int(input('Enter some random length value of the rectangle = '))

breadthnum=int(input('Enter some random breadth value of the rectangle = '))
# Loop till the length of the rectangle using For loop.
for m in range(lengthnum):
    # Loop till the breadth of the rectangle using another nested For loop.
    for n in range(breadthnum):
        # Print the * character inside the nested For loop.
        print('* ', end=' ')
    print()

Output:

Enter some random length value of the rectangle = 9
Enter some random breadth value of the rectangle = 4
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 

2) C++ Implementation

Give the length and breadth of the rectangle as user input using the cin and store them in two separate variables.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the length and breadth of the rectangle as user
    // input using the cin
    // and store them in two separate variables.
    int lengthnum, breadthnum;
    cout << "Enter some random length and breadth of "
            "rectangle separated by spaces"
         << endl;
    cin >> lengthnum >> breadthnum;
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum;
             n++) { // Print the * character inside the
            // nested For loop.
            cout << "* ";
        }
        cout << endl;
    }

    return 0;
}

Output:

Enter some random length and breadth of rectangle separated by spaces
4 16
* * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * 

3) C Implementation

Give the length and breadth of the rectangle as user input using the scanf and store them in two separate variables.

Below is the implementation:

#include <stdio.h>

int main()
{
    //    Give the length and breadth of the rectangle as
    // user input using the scanf
    // and store them in two separate variables.
    int lengthnum,breadthnum ;
    printf("Enter some random length and breadth of rectangle separated by spaces \n");
    scanf("%d%d", &lengthnum, &breadthnum);
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum; n++) {
            // Print the * character inside the
            // nested For loop.
            printf("* ");
        }
        printf("\n");
    }
}

Output:

Enter some random length and breadth of rectangle separated by spaces
8 15
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * *

Related Programs:

Python Program to Print Rectangle Star Pattern Read More »

Python Program to Print Hollow Square Star Pattern

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Given the sides of the square, the task is to print the hollow Square pattern in C, C++, and Python.

Examples:

Example1:

Input:

given number of sides of square =6

Output:

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

Example2:

Input:

given number of sides of square =8
given character to print =+

Output:

+ + + + + + + + 
+                      + 
+                      + 
+                      + 
+                      + 
+                      + 
+                      + 
+ + + + + + + +

Program to Print Hollow Square Star Pattern in C, C++, and Python

Below are the ways to print the hollow square star pattern in C, C++, and Python.

Method #1: Using For loop (Star Character)

Approach:

  • Give the side of the square as static input and store it in a variable.
  • Loop till the side length of the square using For loop.
  • Loop till the side length of the square using another nested For loop.
  • We use the If Else statement to check If the side length is 0 or maximum – 1.
  • If it is true then print * else print space.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the side of the square as static input and store it in a variable.
squareside = 6
# Loop till the side length of the square using For loop.
for m in range(squareside):
    # Loop till the side length of the square using another nested For loop.
    for n in range(squareside):
        # We use the If Else statement to check If the length or breadth number is 0 or maximum – 1.
        # If it is true then print * else print space.
        if(m == 0 or m == squareside - 1 or n == 0 or n == squareside - 1):
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()

Output:

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

2) C++Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the side of the square as static input and store
    // it in a variable.
    int squareside = 19;
    // Loop till the side length of the square using For
    // loop.
    for (int m = 0; m < squareside; m++) {
        // Loop till the side length of the square using
        // another nested For loop.
        for (int n = 0; n < squareside; n++) {
            // We use the If Else statement to check If the
            // side length is 0 or maximum – 1.
            if (m == 0 || m == squareside - 1 || n == 0
                || n == squareside - 1)
                cout << "* ";
            else
                cout << "  ";
        }
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{
    // Give the side of the square as static input and store
    // it in a variable.
    int squareside = 5;
    // Loop till the side length of the square using For
    // loop.
    for (int m = 0; m < squareside; m++) {
        // Loop till the side length of the square using
        // another nested For loop.
        for (int n = 0; n < squareside; n++) {
            // We use the If Else statement to check If the
            // side length is 0 or maximum – 1.
            if (m == 0 || m == squareside - 1 || n == 0
                || n == squareside - 1)
                printf("* ");
            else
                printf("  ");
        }
        printf("\n");
    }
}

Output:

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

Method #2: Using For Loop (User Character)

Approach:

  • Give the side of the square as user input and store it in a variable.
  • Scan the character to print as user input and store it in a variable.
  • Loop till the side length of the square using For loop.
  • Loop till the side length of the square using another nested For loop.
  • We use the If Else statement to check If the side length is 0 or maximum – 1.
  • If it is true then print the given character else print space.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of sides of the square 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 side of the square as user input and store it in a variable.
squareside = int(input('Enter some random number of sides of square = '))
# Scan the character to print as user input and store it in a variable.
characte = input('Enter some random character to print = ')
# Loop till the side length of the square using For loop.
for m in range(squareside):
    # Loop till the side length of the square using another nested For loop.
    for n in range(squareside):
        # We use the If Else statement to check If the length or breadth number is 0 or maximum – 1.
        # If it is true then print the given character else print space.
        if(m == 0 or m == squareside - 1 or n == 0 or n == squareside - 1):
            print(characte, end=' ')
        else:
            print(' ', end=' ')
    print()

Output:

Enter some random number of sides of square = 7
Enter some random character to print = (
( ( ( ( ( ( ( 
(           ( 
(           ( 
(           ( 
(           ( 
(           ( 
( ( ( ( ( ( (

2) C++Implementation

  • Give the number of sides of the square as user input using cin and store it in a variable.
  • Create a character 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(void)
{
    int sidesnum;
    char characte;
    // Give the number of sides of the square as user input
    // using cin and
    // store it in a variable.
    cout << "Enter some random number of sides of the "
            "square = "
         << endl;
    cin >> sidesnum;
    // Create a character variable.
    // Give the character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character to print = "
         << endl;
    cin >> characte;
    cout << endl;

    // Using Nested For loops print the square pattern.
    for (int m = 0; m < sidesnum; m++) {
        for (int n = 0; n < sidesnum; n++) {
            // We use the If Else statement to check If the
            // length or breadth number is 0 or maximum – 1.
            // If it is true then print the given character
            // else print space.
            if (m == 0 || m == sidesnum - 1 || n == 0
                || n == sidesnum - 1)
                cout << characte << " ";
            else
                cout << "  ";
        }
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of sides of the square = 
8
Enter some random character to print = 
+
+ + + + + + + + 
+                      + 
+                      + 
+                      + 
+                      + 
+                      + 
+                      + 
+ + + + + + + +

3) C Implementation

  • Give the number of sides of the square as user input using scanf and store it in a variable.
  • Create a character variable.
  • Give the character as user input using scanf and store it in another variable.

Below is the implementation:

#include <stdio.h>

int main()
{
    int sidesnum;
    char characte;
    // Give the number of sides of the square as user input
    // using scanf and
    // store it in a variable.
    // Create a character variable.
    // Give the character as user input using scanf and
    // store it in another variable.
    // Using Nested For loops print the square pattern.
    scanf("%d%c", &sidesnum, &characte);
    printf("\n");

    for (int m = 0; m < sidesnum; m++) {
        for (int n = 0; n < sidesnum; n++) {
            // We use the If Else statement to check If the
            // length or breadth number is 0 or maximum – 1.
            // If it is true then print the given character
            // else print space.
            if (m == 0 || m == sidesnum - 1 || n == 0
                || n == sidesnum - 1)
                printf("%c ", characte);
            else
                printf("  ");
        }
        printf("\n");
    }
}

Output:

8^
^ ^ ^ ^ ^ ^ ^ ^
^                      ^ 
^                      ^
^                      ^
^                      ^
^                      ^
^                      ^
^ ^ ^ ^ ^ ^ ^ ^

Related Programs:

Python Program to Print Hollow Square Star Pattern Read More »

Python Program to Print Alternate Numbers Pattern using While Loop

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Given the number of rows, the task is to Print Alternate Numbers Pattern using While Loop in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows = 9

Output:

1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9 
11 11 11 11 11 11 
13 13 13 13 13 13 13 
15 15 15 15 15 15 15 15 
17 17 17 17 17 17 17 17 17

Example2:

Input:

Given number of rows = 7

Output:

1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9 
11 11 11 11 11 11 
13 13 13 13 13 13 13

Program to Print Alternate Numbers Pattern using While Loop in C, C++, and Python

Below are the ways to Print Alternate Numbers Pattern using While Loop in C, C++, and Python

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Take a variable(say f ) and initialize it 1.
  • Loop till f is less than or equal to the number of rows using While Loop.
  • Take a variable(say g ) and initialize it 1.
  • Loop till g is is less than or equal to f i.e g<=f using Another While Loop(Nested While Loop).
  • Inside the inner While Loop print the value of 2*f-1 with space.
  • Increment the value of j by 1.
  • After the end of the inner while loop increment the value of variable f by 1.
  • Print the Newline character.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numberrows = 10
# Take a variable(say f ) and initialize it 1.
f = 1
# Loop till f is less than or equal to the number of rows using While Loop.
while(f <= numberrows):
    # Take a variable(say g ) and initialize it 1.
    g = 1
    # Loop till g is is less than or equal to f
    # i.e g<=f using Another While Loop(Nested While Loop).
    while g <= f:
        # Inside the inner While Loop print the value of 2*f-1 with space.
        print((f * 2 - 1), end=" ")
        # Increment the value of g by 1.
        g = g + 1
    # After the end of the inner while loop increment the value of variable f by 1.
    f = f + 1
    # Print the Newline character.
    print()

Output:

1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9 
11 11 11 11 11 11 
13 13 13 13 13 13 13 
15 15 15 15 15 15 15 15 
17 17 17 17 17 17 17 17 17 
19 19 19 19 19 19 19 19 19 19

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows as static input and store it
    // in a variable.
    int numberrows = 7;

    // Take a variable(say f ) and initialize it 1.
    int f = 1;
    // Loop till f is less than or equal to the number of
    // rows using While Loop.
    while (f <= numberrows) {
        // Take a variable(say g ) and initialize it 1.
        int g = 1;
        // Loop till g is is less than or equal to f
        // i.e g<=f using Another While Loop(Nested While
        // Loop).
        while (g <= f) {
            // Inside the inner While Loop print the value
            // of 2*f-1 with space.
            cout << (f * 2 - 1) << " ";
            // Increment the value of g by 1.
            g = g + 1;
        }
        // After the end of the inner while loop increment
        // the value of variable f by 1.
        f = f + 1;
        // Print the Newline character.
        cout << endl;
    }

    return 0;
}

Output:

1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9 
11 11 11 11 11 11 
13 13 13 13 13 13 13

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows as static input and store it
    // in a variable.
    int numberrows = 7;

    // Take a variable(say f ) and initialize it 1.
    int f = 1;
    // Loop till f is less than or equal to the number of
    // rows using While Loop.
    while (f <= numberrows) {
        // Take a variable(say g ) and initialize it 1.
        int g = 1;
        // Loop till g is is less than or equal to f
        // i.e g<=f using Another While Loop(Nested While
        // Loop).
        while (g <= f) {
            // Inside the inner While Loop print the value
            // of 2*f-1 with space.
            printf("%d ", (f * 2 - 1));
            // Increment the value of g by 1.
            g = g + 1;
        }
        // After the end of the inner while loop increment
        // the value of variable f by 1.
        f = f + 1;
        // Print the Newline character.
        printf("\n");
    }
    return 0;
}

Output:

1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9 
11 11 11 11 11 11 
13 13 13 13 13 13 13

Method #2: Using While Loop (User Input)

Approach:

  • Give the number of rows as user input and store it in a variable.
  • Take a variable(say f ) and initialize it 1.
  • Loop till f is less than or equal to the number of rows using While Loop.
  • Take a variable(say g ) and initialize it 1.
  • Loop till g is is less than or equal to f i.e g<=f using Another While Loop(Nested While Loop).
  • Inside the inner While Loop print the value of 2*f-1 with space.
  • Increment the value of j by 1.
  • After the end of the inner while loop increment the value of variable f by 1.
  • Print the Newline character.
  • 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.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numberrows = int(input('Enter some random number of rows = '))
# Take a variable(say f ) and initialize it 1.
f = 1
# Loop till f is less than or equal to the number of rows using While Loop.
while(f <= numberrows):
    # Take a variable(say g ) and initialize it 1.
    g = 1
    # Loop till g is is less than or equal to f
    # i.e g<=f using Another While Loop(Nested While Loop).
    while g <= f:
        # Inside the inner While Loop print the value of 2*f-1 with space.
        print((f * 2 - 1), end=" ")
        # Increment the value of g by 1.
        g = g + 1
    # After the end of the inner while loop increment the value of variable f by 1.
    f = f + 1
    # Print the Newline character.
    print()

Output:

Enter some random number of rows = 10
1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9 
11 11 11 11 11 11 
13 13 13 13 13 13 13 
15 15 15 15 15 15 15 15 
17 17 17 17 17 17 17 17 17 
19 19 19 19 19 19 19 19 19 19

2) C++ Implementation

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

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows as user input using
    // int(input()) and store it in a variable.
    int numberrows;
    cin >> numberrows;
    // Take a variable(say f ) and initialize it 1.
    int f = 1;
    // Loop till f is less than or equal to the number of
    // rows using While Loop.
    while (f <= numberrows) {
        // Take a variable(say g ) and initialize it 1.
        int g = 1;
        // Loop till g is is less than or equal to f
        // i.e g<=f using Another While Loop(Nested While
        // Loop).
        while (g <= f) {
            // Inside the inner While Loop print the value
            // of 2*f-1 with space.
            cout << (f * 2 - 1) << " ";
            // Increment the value of g by 1.
            g = g + 1;
        }
        // After the end of the inner while loop increment
        // the value of variable f by 1.
        f = f + 1;
        // Print the Newline character.
        cout << endl;
    }
    return 0;
}

Output:

8
1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9 
11 11 11 11 11 11 
13 13 13 13 13 13 13 
15 15 15 15 15 15 15 15 

3) C Implementation

Give the number of rows as user input using scanf and store it in a 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 numberrows;
    scanf("%d", &numberrows);
    // Take a variable(say f ) and initialize it 1.
    int f = 1;
    // Loop till f is less than or equal to the number of
    // rows using While Loop.
    while (f <= numberrows) {
        // Take a variable(say g ) and initialize it 1.
        int g = 1;
        // Loop till g is is less than or equal to f
        // i.e g<=f using Another While Loop(Nested While
        // Loop).
        while (g <= f) {
            // Inside the inner While Loop print the value
            // of 2*f-1 with space.
            printf("%d ", (f * 2 - 1));
            // Increment the value of g by 1.
            g = g + 1;
        }
        // After the end of the inner while loop increment
        // the value of variable f by 1.
        f = f + 1;
        // Print the Newline character.
        printf("\n");
    }
    return 0;
}

Output:

9
1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9 
11 11 11 11 11 11 
13 13 13 13 13 13 13 
15 15 15 15 15 15 15 15 
17 17 17 17 17 17 17 17 17 

Related Programs:

Related Programs:

Python Program to Print Alternate Numbers Pattern using While Loop Read More »

Python Program to Print Hollow Inverted Right Triangle

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Given the number of rows of the Triangle, the task is to Print a Hollow Inverted Right Triangle Star Pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows of the Hollow Inverted Right Triangle Pattern =7

Output:

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

 

Example2:

Input:

Given number of rows of the Hollow Inverted Right Triangle Pattern =9
Given character to print ='^'

Output:

^^^^^^^^^
^               ^
^            ^
^          ^
^        ^
^      ^
^    ^
^ ^
^

Program to Print Hollow Inverted Right Triangle Star Pattern in C, C++, and Python

Below are the ways to print a Hollow Inverted Right Triangle Star Pattern in C, C++, and Python.

Method #1: Using For Loop(Star Character)

Approach:

  • Give the number of rows of the Hollow Inverted Right Triangle as static input and store it in a variable.
  • Loop from the given number of rows to 0 using For loop and take iterator value as m.
  • Loop from the iterator value of the first loop to 0 using another nested For loop.
  • If m equals zero, rows, n, or n equals one value, the if statement prints stars.
  • Else print space character.
  • Print the newline character after inner for loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the Hollow Inverted Right Triangle as static input and store it in a variable.
triNumRows = 7
# Loop from the given number of rows to 0 using For loop and take iterator value as m.
for m in range(triNumRows, 0, -1):
    # Loop from the  iterator value of the first loop to 0 using another nested For loop.
    for n in range(m, 0, -1):
        # If m equals zero, rows, n, or n equals one value, the if statement prints stars.
        if m == 1 or m == triNumRows or n == 1 or n == m:
            print('*', end='')
        else:
            print(' ', end='')
    # Print the newline character after 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 Inverted Right
    // Triangle as static input and store it in a variable.
    int triNumRows = 7;
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m > 0; m--) {

        // Loop from the iterator value of the first loop to
        // 0 using another nested For loop.
        for (int n = m; n > 0; n--) {
            // If m equals zero, rows, n, or n equals one
            // value, the if statement prints stars.
            if (m == 1 || m == triNumRows || n == 1
                || n == m)
                cout << "* ";
            else
                cout << "  ";
        }
        // Print the newline character after 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 Inverted Right
    // Triangle as static input and store it in a variable.
    int triNumRows = 7;
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m > 0; m--) {

        // Loop from the iterator value of the first loop to
        // 0 using another nested For loop.
        for (int n = m; n > 0; n--) {
            // If m equals zero, rows, n, or n equals one
            // value, the if statement prints stars.
            if (m == 1 || m == triNumRows || n == 1
                || n == m)
                printf("* ");
            else
                printf("  ");
        }
        // Print the newline character after inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For Loop(User Character)

Approach:

  • Give the number of rows of the Hollow Inverted Right Triangle as static input and store it in a variable.
  • Give the character to print as user input and store it in a variable.
  • Loop from the given number of rows to 0 using For loop and take iterator value as m.
  • Loop from the iterator value of the first loop to 0 using another nested For loop.
  • If m equals zero, rows, n, or n equals one value, the if statement prints stars.
  • Else print space character.
  • Print the newline character after inner for loop.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of rows of the Hollow Inverted Right Triangle 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 of the Inverted Right Triangle as user input using int(input()) and store it in a variable.
triNumRows = int(input(
    'Enter some random number of rows of the Inverted Right Triangle Pattern = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Loop from the given number of rows to 0 using For loop and take iterator value as m.
for m in range(triNumRows, 0, -1):
    # Loop from the  iterator value of the first loop to 0 using another nested For loop.
    for n in range(m, 0, -1):
        # If m equals zero, rows, n, or n equals one value, the if statement prints stars.
        if m == 1 or m == triNumRows or n == 1 or n == m:
            print(givencharacter, end='')
        else:
            print(' ', end='')
    # Print the newline character after inner for loop.
    print()

Output:

Enter some random number of rows of the Inverted Right Triangle Pattern = 9
Enter some random character = ^
^^^^^^^^^
^               ^
^            ^
^          ^
^        ^
^      ^
^    ^
^ ^
^

2) C++ Implementation

  • Give the number of rows of the Hollow Inverted Right Triangle 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 of the Inverted Right
    // Triangle as user input using cin and store it in a
    // variable.
    int triNumRows;
    char givencharacter;
    cout << "Enter some random number of rows of the "
            "Inverted Right Triangle Pattern = "
         << endl;
    cin >> triNumRows;
    // 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 the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m > 0; m--) {

        // Loop from the iterator value of the first loop to
        // 0 using another nested For loop.
        for (int n = m; n > 0; n--) {
            // If m equals zero, rows, n, or n equals one
            // value, the if statement prints stars.
            if (m == 1 || m == triNumRows || n == 1
                || n == m)
                cout << givencharacter << " ";
            else
                cout << "  ";
        }
        // Print the newline character after inner for loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the Inverted Right Triangle Pattern = 9
Enter some random character = ^
^^^^^^^^^
^               ^
^            ^
^          ^
^        ^
^      ^
^    ^
^ ^
^

3) C Implementation

  • Give the number of rows of the Hollow Inverted Right Triangle 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 of the Inverted Right
    // Triangle as user input using scanf and store it in a
    // variable.
    int triNumRows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.

    scanf("%d", &triNumRows);
    scanf("%c", &givencharacter);
    printf("\n");
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m > 0; m--) {

        // Loop from the iterator value of the first loop to
        // 0 using another nested For loop.
        for (int n = m; n > 0; n--) {
            // If m equals zero, rows, n, or n equals one
            // value, the if statement prints stars.
            if (m == 1 || m == triNumRows || n == 1
                || n == m)
                printf("%c ", givencharacter);
            else
                printf("  ");
        }
        // Print the newline character after inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

9^
^^^^^^^^^
^               ^
^            ^
^          ^
^        ^
^      ^
^    ^
^ ^
^

Related Programs:

Python Program to Print Hollow Inverted Right Triangle Read More »

Python Program to Print Diamond Star Pattern

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Given the number of rows of the diamond pattern, the task is to print the diamond star pattern in C, C++, and Python

Examples:

Example1:

Input:

given number of rows of diamond =5

Output:

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

Example2:

Input:

given number of rows of diamond =7
given character to print ='$'

Output:

          $
        $$$
       $$$$$
     $$$$$$$
   $$$$$$$$$
  $$$$$$$$$$$
$$$$$$$$$$$$$
  $$$$$$$$$$$
    $$$$$$$$$
      $$$$$$$
        $$$$$
         $$$
           $

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

Below are the ways to print Diamond Star Pattern in C, C++, and Python.

Method #1: Using For loop (Star Character)

Approach:

  • Give the number of rows of the diamond pattern as static input and store it in a variable.
  • Loop from 1 to the number of rows using For Loop.
  • Loop from 1 to the number of rows -iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the 2* iterator value – 1 of the parent For loop using another For loop(Nested For loop).
  • Print the star character.
  • After the end of the inner for Loops print the Newline Character.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the space character in the inner For loop.
  • Loop from 1 to 2*(number of rows – iterator value of the parent for loop) using another For loop(Nested For loop).
  • Print the star character.
  • After the end of the inner for Loops print the Newline Character.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the diamond pattern as static input and store it in a variable.

diamondrows = 5
# Loop from 1 to the number of rows using For Loop.
for m in range(1, diamondrows+1):
    # Loop from 1 to the number of rows -iterator value of the parent
    # For loop using another For loop(Nested For loop).
    for n in range(1, diamondrows - m + 1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 0 to the 2* iterator value - 1 of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(0, (2 * m) - 1):
        # Print the star character
        print('*', end='')
    # After the end of the inner for Loops print the Newline Character.
    print()

# Loop from 1 to the number of rows using For loop.
for m in range(1, diamondrows):
    # Loop from 1 to iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for n in range(1, m+1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 1 to 2*(number of rows - iterator value of the parent for loop)
    # using another For loop(Nested For loop).
    for l in range(1, (2 * (diamondrows - m))):
        # Print the star character
        print('*', end='')

    # After the end of the inner for Loops print the Newline Character.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows of the diamond pattern
    // as static input and store it in a variable.

    int diamondrows = 5;
    // Loop from 1 to the number of rows using For Loop.
    for (int m = 1; m <= diamondrows; m++) {

        // Loop from 1 to the number of rows- iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 1; n <= (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 0 to the 2 * iterator value- 1 of the
        // parent For loop using another For loop(Nested For
        // loop).
        for (int l = 0; l < ((2 * m) - 1); l++) {
            // Print the star character
            cout << "*";
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        cout << endl;
    }
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m < diamondrows; m++) {
        // Loop from 1 to iterator value of the parent For
        // loop using another For loop(Nested For loop).
        for (int n = 1; n <= m; n++) {
            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 1 to 2* (number of rows- iterator value
        // of the parent for loop) using another For
        // loop(Nested For loop).
        for (int l = 1; l < (2 * (diamondrows - m)); l++) {
            // Print the star character
            cout << "*";
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{ // Give the number of rows of the diamond pattern
    // as static input and store it in a variable.

    int diamondrows = 5;
    // Loop from 1 to the number of rows using For Loop.
    for (int m = 1; m <= diamondrows; m++) {

        // Loop from 1 to the number of rows- iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 1; n <= (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 0 to the 2 * iterator value- 1 of the
        // parent For loop using another For loop(Nested For
        // loop).
        for (int l = 0; l < ((2 * m) - 1); l++) {
            // Print the star character
            printf("*");
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        printf("\n");
    }
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m < diamondrows; m++) {
        // Loop from 1 to iterator value of the parent For
        // loop using another For loop(Nested For loop).
        for (int n = 1; n <= m; n++) {
            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 1 to 2* (number of rows- iterator value
        // of the parent for loop) using another For
        // loop(Nested For loop).
        for (int l = 1; l < (2 * (diamondrows - m)); l++) {
            // Print the star character
            printf("*");
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For loop (User Character)

Approach:

  • Give the number of rows of the diamond 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 1 to the number of rows using For Loop.
  • Loop from 1 to the number of rows -iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the 2* iterator value – 1 of the parent For loop using another For loop(Nested For loop).
  • Print the given character.
  • After the end of the inner for Loops print the Newline Character.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the space character in the inner For loop.
  • Loop from 1 to 2*(number of rows – iterator value of the parent for loop) using another For loop(Nested For loop).
  • Print the given character.
  • After the end of the inner for Loops print the Newline Character.
  • 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.
diamondrows = 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 1 to the number of rows using For Loop.
for m in range(1, diamondrows+1):
    # Loop from 1 to the number of rows -iterator value of the parent
    # For loop using another For loop(Nested For loop).
    for n in range(1, diamondrows - m + 1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 0 to the 2* iterator value - 1 of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(0, (2 * m) - 1):
        # Print the given character
        print(givencharacter, end='')
    # After the end of the inner for Loops print the Newline Character.
    print()

# Loop from 1 to the number of rows using For loop.
for m in range(1, diamondrows):
    # Loop from 1 to iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for n in range(1, m+1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 1 to 2*(number of rows - iterator value of the parent for loop)
    # using another For loop(Nested For loop).
    for l in range(1, (2 * (diamondrows - m))):
        # Print the given character
        print(givencharacter, end='')

    # After the end of the inner for Loops print the Newline Character.
    print()

Output:

Enter some random number of rows = 7
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 diamondrows;
    char givencharacter;
    cout << "Enter some random number of rows = " << endl;
    cin >> diamondrows;
    // 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 1 to the number of rows using For Loop.
    for (int m = 1; m <= diamondrows; m++) {

        // Loop from 1 to the number of rows- iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 1; n <= (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 0 to the 2 * iterator value- 1 of the
        // parent For loop using another For loop(Nested For
        // loop).
        for (int l = 0; l < ((2 * m) - 1); l++) {
            // Print the given character
            cout << givencharacter;
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        cout << endl;
    }
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m < diamondrows; m++) {
        // Loop from 1 to iterator value of the parent For
        // loop using another For loop(Nested For loop).
        for (int n = 1; n <= m; n++) {
            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 1 to 2* (number of rows- iterator value
        // of the parent for loop) using another For
        // loop(Nested For loop).
        for (int l = 1; l < (2 * (diamondrows - m)); l++) {
            // Print the  given character
            cout << givencharacter;
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows = 
7
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 diamondrows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &diamondrows);
    scanf("%c", &givencharacter);
    // Loop from 1 to the number of rows using For Loop.
    for (int m = 1; m <= diamondrows; m++) {

        // Loop from 1 to the number of rows- iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 1; n <= (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 0 to the 2 * iterator value- 1 of the
        // parent For loop using another For loop(Nested For
        // loop).
        for (int l = 0; l < ((2 * m) - 1); l++) {
            // Print the given character
            printf("%c", givencharacter);
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        printf("\n");
    }
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m < diamondrows; m++) {
        // Loop from 1 to iterator value of the parent For
        // loop using another For loop(Nested For loop).
        for (int n = 1; n <= m; n++) {
            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 1 to 2* (number of rows- iterator value
        // of the parent for loop) using another For
        // loop(Nested For loop).
        for (int l = 1; l < (2 * (diamondrows - m)); l++) {
            // Print the givencharacter
            printf("%c", givencharacter);
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        printf("\n");
    }
    return 0;
}

Output:

7$
          $
        $$$
       $$$$$
     $$$$$$$
   $$$$$$$$$
  $$$$$$$$$$$
$$$$$$$$$$$$$
  $$$$$$$$$$$
    $$$$$$$$$
      $$$$$$$
        $$$$$
         $$$
           $

Related Programs:

Python Program to Print Diamond Star Pattern Read More »

Python Program to Print Hollow Half Diamond Star Pattern

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Given the number of rows of the diamond pattern, the task is to print the Hollow Half diamond Pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows =7

Output:

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

Example2:

Input:

Given number of rows =9
Given Character to print ='-'

Output:

- 
- - 
-   - 
-     - 
-       - 
-         - 
-           - 
-         - 
-       - 
-     - 
-   - 
- - 
-

Program to Print Hollow Half Diamond Star Pattern in C, C++, and Python

Below are the ways to print Hollow Half Diamond Star Pattern in C, C++, and python.

Method #1: Using For loop (Star Character)

Approach:

  • Give the number of rows of the number of diamond pattern as static input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Loop till the first iterator value using another For loop(Nested For loop).
  • Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
  • Check if the second loop iterator value is equal to 0 using the If Statement.
  • Combine these two conditions using or operator.
  • If the statement is true then print star character with space.
  • Else print space character.
  • After the end of the inner for loop print the Newline Character.
  • After the end of two For loops Loop from 1 to the number of rows using For loop.
  • Loop from the first iterator value to the given number of rows using another For loop(Nested For loop).
  • Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
  • Check if the second loop iterator value is equal to the number of rows using the If Statement.
  • Combine these two conditions using or operator.
  • If the statement is true then print star character with space.
  • Else print space character.
  • After the end of the inner for loop print the Newline Character.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the number of diamond pattern as static input and store it in a variable.
rowsnumber = 7
# Loop from 0 to the number of rows using For loop.
for m in range(0, rowsnumber):
        # Loop till the first iterator value using another For loop(Nested For loop).
    for n in range(0, m+1):
        # Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
        # Check if the second loop iterator value is equal to 0 using the If Statement.
        # Combine these two conditions using or operator.
        if(m == n or n == 0):
            # If the statement is true then print star character with space.
            print("*", end=" ")
          # Else print space character.
        else:
            print(' ', end=' ')
    # After the end of the inner for loop print the Newline Character.
    print()
# After the end of two For loops Loop from 1 to the number of rows using For loop.
for m in range(1, rowsnumber):
    # Loop from the first iterator value to the given number of rows using another For loop(Nested For loop).
    for n in range(m, rowsnumber):
        # Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
                # Check if the second loop iterator value is equal to the number of rows using the If Statement.
                # Combine these two conditions using or operator.
        if(m == n or n == rowsnumber-1):
         # If the statement is true then print star character with space.
            print('*', end=' ')
          # Else print space character.
        else:
            print(' ', end=' ')

    # After the end of the inner for loop print the Newline Character.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    //Give the number of rows of the number of diamond pattern  \
  //  as static input and store it in a variable.
    int rowsnumber = 7;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < rowsnumber; m++) {
        // Loop till the first iterator value using another
        // For loop(Nested For loop)
        for (int n = 0; n < m + 1; n++) {
            /*
            Check if the first loop iterator value is equal
            to the second loop iterator value using the If
            Statement. Check if the second loop iterator
            value is equal to 0 using the If Statement.
              Combine these two conditions using or
            operator. If the statement is true then print
            star character with space. Else print space
            character.*/
            if (m == n || n == 0)
                cout << "* ";
            else
                cout << "  ";
        }
        // After the end of the inner for loop print the
        // Newline Character.
        cout << endl;
    }
    // After the end of two For loops Loop from 1 to the
    // number of rows using For loop.
    for (int m = 1; m < rowsnumber; m++)
    // Loop from the first iterator value to the given
    // number of rows using another For loop(Nested For
    // loop)
    {
        for (int n = m; n < rowsnumber; n++) {

            /*
             Check if the first loop iterator value is equal
             to the second loop iterator value using the If
             Statement. Check if the second loop iterator
             value is equal to the number of rows using the
             If Statement. Combine these two conditions
             using or operator. If the statement is true
             then print star character with space. Else
             print space character.*/
            if (m == n || n == rowsnumber - 1)
                cout << "* ";
            else
                cout << "  ";
        }
        // After the end of the inner for loop print the
        // Newline Character.
        cout << endl;
    }
    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    //Give the number of rows of the number of diamond pattern  \
  //  as static input and store it in a variable.
    int rowsnumber = 7;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < rowsnumber; m++) {
        // Loop till the first iterator value using another
        // For loop(Nested For loop)
        for (int n = 0; n < m + 1; n++) {

            /*
           Check if the first loop iterator value is equal
           to the second loop iterator value using the If
           Statement. Check if the second loop iterator
           value is equal to 0 using the If Statement.
             Combine these two conditions using or
           operator. If the statement is true then print
           star character with space. Else print space
           character.*/
            if (m == n || n == 0)
                printf("* ");
            else
                printf("  ");
        }
        // After the end of the inner for loop print the
        // Newline Character.
        printf("\n");
    }
    // After the end of two For loops Loop from 1 to the
    // number of rows using For loop.
    for (int m = 1; m < rowsnumber; m++)
    // Loop from the first iterator value to the given
    // number of rows using another For loop(Nested For
    // loop)
    {
        for (int n = m; n < rowsnumber; n++) {

            /*
             Check if the first loop iterator value is equal
             to the second loop iterator value using the If
             Statement. Check if the second loop iterator
             value is equal to the number of rows using the
             If Statement. Combine these two conditions
             using or operator. If the statement is true
             then print star character with space. Else
             print space character.*/
            if (m == n || n == rowsnumber - 1)
                printf("* ");
            else
                printf("  ");
        }
        // After the end of the inner for loop print the
        // Newline Character.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For loop (User Character)

Approach:

  • Give the number of rows of the number of diamond 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 till the first iterator value using another For loop(Nested For loop).
  • Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
  • Check if the second loop iterator value is equal to 0 using the If Statement.
  • Combine these two conditions using or operator.
  • If the statement is true then print the given character with space.
  • Else print space character.
  • After the end of the inner for loop print the Newline Character.
  • After the end of two For loops Loop from 1 to the number of rows using For loop.
  • Loop from the first iterator value to the given number of rows using another For loop(Nested For loop).
  • Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
  • Check if the second loop iterator value is equal to the number of rows using the If Statement.
  • Combine these two conditions using or operator.
  • If the statement is true then print the given character with space.
  • Else print space character.
  • After the end of the inner for loop print the Newline Character.
  • 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.
rowsnumber = 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, rowsnumber):
        # Loop till the first iterator value using another For loop(Nested For loop).
    for n in range(0, m+1):
        # Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
        # Check if the second loop iterator value is equal to 0 using the If Statement.
        # Combine these two conditions using or operator.
        if(m == n or n == 0):
            # If the statement is true then print the given character with space.
            print(givencharacter, end=" ")
          # Else print space character.
        else:
            print(' ', end=' ')
    # After the end of the inner for loop print the Newline Character.
    print()
# After the end of two For loops Loop from 1 to the number of rows using For loop.
for m in range(1, rowsnumber):
    # Loop from the first iterator value to the given number of rows using another For loop(Nested For loop).
    for n in range(m, rowsnumber):
        # Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
                # Check if the second loop iterator value is equal to the number of rows using the If Statement.
                # Combine these two conditions using or operator.
        if(m == n or n == rowsnumber-1):
         # If the statement is true then print the given character with space.
            print(givencharacter, end=' ')
          # Else print space character.
        else:
            print(' ', end=' ')

    # After the end of the inner for loop print the Newline Character.
    print()

Output:

Enter some random number of rows = 9
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 rowsnumber;
    char givencharacter;
    cout << "Enter some random number of rows = " << endl;
    cin >> rowsnumber;
    // 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 < rowsnumber; m++) {
        // Loop till the first iterator value using another
        // For loop(Nested For loop)
        for (int n = 0; n < m + 1; n++) {
            /*
            Check if the first loop iterator value is equal
            to the second loop iterator value using the If
            Statement. Check if the second loop iterator
            value is equal to 0 using the If Statement.
              Combine these two conditions using or
            operator. If the statement is true then print
            given character with space. Else print space
            character.*/
            if (m == n || n == 0)
                cout << givencharacter << " ";
            else
                cout << "  ";
        }
        // After the end of the inner for loop print the
        // Newline Character.
        cout << endl;
    }
    // After the end of two For loops Loop from 1 to the
    // number of rows using For loop.
    for (int m = 1; m < rowsnumber; m++)
    // Loop from the first iterator value to the given
    // number of rows using another For loop(Nested For
    // loop)
    {
        for (int n = m; n < rowsnumber; n++) {

            /*
             Check if the first loop iterator value is equal
             to the second loop iterator value using the If
             Statement. Check if the second loop iterator
             value is equal to the number of rows using the
             If Statement. Combine these two conditions
             using or operator. If the statement is true
             then print given character with space. Else
             print space character.*/
            if (m == n || n == rowsnumber - 1)
                cout << givencharacter << " ";
            else
                cout << "  ";
        }
        // After the end of the inner for loop print the
        // Newline Character.
        cout << endl;
    }
    return 0;
}

Output:

Enter some random number of rows = 9
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 rowsnumber;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &rowsnumber);
    scanf("%c", &givencharacter);
 
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < rowsnumber; m++) {
        // Loop till the first iterator value using another
        // For loop(Nested For loop)
        for (int n = 0; n < m + 1; n++) {

            /*
           Check if the first loop iterator value is equal
           to the second loop iterator value using the If
           Statement. Check if the second loop iterator
           value is equal to 0 using the If Statement.
             Combine these two conditions using or
           operator. If the statement is true then print
           given character with space. Else print space
           character.*/
            if (m == n || n == 0)
                printf("%c ", givencharacter);
            else
                printf("  ");
        }
        // After the end of the inner for loop print the
        // Newline Character.
        printf("\n");
    }
    // After the end of two For loops Loop from 1 to the
    // number of rows using For loop.
    for (int m = 1; m < rowsnumber; m++)
    // Loop from the first iterator value to the given
    // number of rows using another For loop(Nested For
    // loop)
    {
        for (int n = m; n < rowsnumber; n++) {

            /*
             Check if the first loop iterator value is equal
             to the second loop iterator value using the If
             Statement. Check if the second loop iterator
             value is equal to the number of rows using the
             If Statement. Combine these two conditions
             using or operator. If the statement is true
             then print given character with space. Else
             print space character.*/
            if (m == n || n == rowsnumber - 1)
                printf("%c ", givencharacter);
            else
                printf("  ");
        }
        // After the end of the inner for loop print the
        // Newline Character.
        printf("\n");
    }
    return 0;
}

Output:

9-
- 
- - 
-   - 
-     - 
-       - 
-         - 
-           - 
-         - 
-       - 
-     - 
-   - 
- - 
-

Related Programs:

Python Program to Print Hollow Half Diamond Star Pattern Read More »

Python Program to Print Inverted Right Triangle Star Pattern

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Given the number of rows of the Triangle, the task is to Print an Inverted Right Triangle Star Pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows of the Inverted Right Triangle Pattern =13

Output:

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

Example2:

Input:

Given number of rows of the Inverted Right Triangle Pattern =9
Given character to print ='<'

Output:

< < < < < < < < < 
< < < < < < < < 
< < < < < < < 
< < < < < < 
< < < < < 
< < < < 
< < < 
< < 
<

Program to Print Inverted Right Triangle Star Pattern in C, C++, and Python

Below are the ways to print an Inverted Right Triangle Star Pattern in C, C++, and Python.

Method #1: Using For Loop(Star Character)

Approach:

  • Give the number of rows of the Inverted Right Triangle as static input and store it in a variable.
  • Loop from the given number of rows to 0 using For loop and take iterator value as m.
  • Loop till the iterator value of the first loop using another nested For loop.
  • Print the star and space character in the inner for loop.
  • Print the newline character after inner for loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the Inverted Right Triangle as static input and store it in a variable.
triNumRows = 13
# Loop from the given number of rows to 0 using For loop and take iterator value as m.
for m in range(triNumRows, -1, -1):
    # Loop till the iterator value of the first loop using another nested For loop.
    for n in range(m):
        # Print the star and space character in the inner for loop.
        print('*', end=' ')
    # Print the newline character after 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 Inverted Right
    // Triangle as static input and store it in a variable.
    int triNumRows = 5;
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m >= 0; m--) {

        // Loop till the iterator value of the first loop
        // using another nested For loop.
        for (int n = 0; n < m; n++) {
            // Print the star and space character in the
            // inner for loop.
            cout << "* ";
        }
        // Print the newline character after 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 Inverted Right
    // Triangle as static input and store it in a variable.
    int triNumRows = 13;
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m >= 0; m--) {

        // Loop till the iterator value of the first loop
        // using another nested For loop.
        for (int n = 0; n < m; n++) {
            // Print the star and space character in the
            // inner for loop.
            printf("* ");
        }
        // Print the newline character after inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For Loop(User Character)

Approach:

  • Give the number of rows of the Inverted Right Triangle as static input and store it in a variable.
  • Give the character to print as user input and store it in a variable.
  • Loop from the given number of rows to 0 using For loop and take iterator value as m.
  • Loop till the iterator value of the first loop using another nested For loop.
  • Print the given character and space character in the inner for loop.
  • Print the newline character after inner for loop.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of rows of the Inverted Right Triangle 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 of the Inverted Right Triangle as user input using int(input()) and store it in a variable.
triNumRows = int(input(
    'Enter some random number of rows of the Inverted Right Triangle Pattern = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Loop from the given number of rows to 0 using For loop and take iterator value as m.
for m in range(triNumRows, -1, -1):
    # Loop till the iterator value of the first loop using another nested For loop.
    for n in range(m):
        # Print the given character with the space
        print(givencharacter, end=' ')
    # Print the newline character after inner for loop.
    print()

Output:

Enter some random number of rows of the Inverted Right Triangle Pattern = 7
Enter some random character = @
@ @ @ @ @ @ @ 
@ @ @ @ @ @ 
@ @ @ @ @ 
@ @ @ @ 
@ @ @ 
@ @ 
@

2) C++ Implementation

  • Give the number of rows of the Inverted Right Triangle 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 of the Inverted Right
    // Triangle as user input using cin and store it in a
    // variable.
    int triNumRows;
    char givencharacter;
    cout << "Enter some random number of rows of the "
            "Inverted Right Triangle Pattern = "
         << endl;
    cin >> triNumRows;
    // 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 the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m >= 0; m--) {

        // Loop till the iterator value of the first loop
        // using another nested For loop.
        for (int n = 0; n < m; n++) {
            // Print the given character with the space
            cout << givencharacter << " ";
        }
        // Print the newline character after inner for loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the Inverted Right Triangle Pattern = 
15
Enter some random character = 
^
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ 
^ ^ ^ ^ 
^ ^ ^ 
^ ^ 
^

3) C Implementation

  • Give the number of rows of the Inverted Right Triangle 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 of the Inverted Right
    // Triangle as user input using scanf and store it in a
    // variable.
    int triNumRows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.

    scanf("%d", &triNumRows);
    scanf("%c", &givencharacter);
    printf("\n");
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m >= 0; m--) {

        // Loop till the iterator value of the first loop
        // using another nested For loop.
        for (int n = 0; n < m; n++) {
            // Print the given character and space character
            // in the inner for loop.
            printf("%c ", givencharacter);
        }
        // Print the newline character after inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

9<
< < < < < < < < < 
< < < < < < < < 
< < < < < < < 
< < < < < < 
< < < < < 
< < < < 
< < < 
< < 
<

Related Programs:

Python Program to Print Inverted Right Triangle Star Pattern Read More »