Python

Python Program to Print Hollow Box Pattern of Numbers

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 and number of columns of the box, the task is to print the Hollow Box Number pattern in C, C++, and python.

Examples:

Example1:

Input:

given number of rows of the box =11
given number of columns of the box =19

Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Example2:

Input:

given number of rows of the box =11
given number of columns of the box =19
given character to print ='&'

Output:

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

Program to Print Hollow Box Number Pattern in C, C++, and Python

Below are the ways to Print Hollow Box Number Pattern in C, C++, and python.

Method #1: Using For Loop (Static Character)

Approach:

  • Give the number of rows and number of columns of the box as static or user input and store them in two separate variables.
  • Loop from 1 to the number of rows of the box +1 using a For loop.
  • Loop from 1 to the number of columns of the box +1 using another Nested For loop.
  • Check if the parent iterator value is equal to 1 or the number of rows of the box using the If statement.
  • Check if the inner loop iterator value is equal to 1 or the number of columns of the box using the If statement.
  • Merge these two conditions in a single If statement using or operator.
  • If the condition is true then print 1 with a space character.
  • Else print space character.
  • Print the newline character after the end of the inner For loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows and number of columns of the box as static or user input
# and store them in two separate variables.
boxrows = 11
boxcolumns = 19
# Loop from 1 to the number of rows of the box +1 using a For loop.
for m in range(1, boxrows+1):
    # Loop from 1 to the number of columns of the box +1 using another Nested For loop.
    for n in range(1, boxcolumns+1):
        '''Check if the parent iterator value is equal to 1 or the number of rows of the box using the If statement.
            Check if the inner loop iterator value is equal to 1 or the number of columns of the box using the If statement.
            Merge these two conditions in a single If statement using or operator.       
                  '''
        if(m == 1 or m == boxrows or n == 1 or n == boxcolumns):
            # If the condition is true then print 1 with a space character.
            print("1", end=" ")
        else:
            # Else print space character.
            print(" ", end=" ")
    # Print the newline character after the end of the inner For loop.
    print()

Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows and number of columns of the
    // box as static or user input and store them in two
    // separate variables.
    int boxrows = 11;
    int boxcolumns = 19;
    // Loop from 1 to the number of rows of the box +1 using
    // a For loop.
    for (int m = 1; m <= boxrows; m++) {
        // Loop from 1 to the number of columns of the box
        // +1 using another Nested For loop.
        for (int n = 1; n <= boxcolumns; n++) {
            /*Check if the parent iterator value is equal to
               1 or the number of rows of the box using the
               If statement. Check if the inner loop
               iterator value is equal to 1 or the number of
               columns of the box using the If statement.
                Merge these two conditions in a single If
               statement using or operator. */
            if (m == 1 || m == boxrows || n == 1
                || n == boxcolumns) {
                // If the condition is true then print 1
                // with a space character.
                cout << "1 ";
            }
            else {
                //  Else print space character.
                cout << "  ";
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        cout << endl;
    }

    return 0;
}

Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows and number of columns of the
    // box as static or user input and store them in two
    // separate variables.
    int boxrows = 11;
    int boxcolumns = 19;
    // Loop from 1 to the number of rows of the box +1 using
    // a For loop.
    for (int m = 1; m <= boxrows; m++) {
        // Loop from 1 to the number of columns of the box
        // +1 using another Nested For loop.
        for (int n = 1; n <= boxcolumns; n++) {
            /*Check if the parent iterator value is equal to
               1 or the number of rows of the box using the
               If statement. Check if the inner loop
               iterator value is equal to 1 or the number of
               columns of the box using the If statement.
                Merge these two conditions in a single If
               statement using or operator. */
            if (m == 1 || m == boxrows || n == 1
                || n == boxcolumns) {
                // If the condition is true then print 1
                // with a space character.
                printf("1 ");
            }
            else {
                // Else print space character.
                printf("  ");
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        printf("\n");
    }
    return 0;
}

Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Method #2: Using For Loop (User Character)

Approach:

  • Give the number of rows and number of columns of the box as user input and store them in two separate variables.
  • Give the character to print as a box pattern as user input and store it in a variable.
  • Loop from 1 to the number of rows of the box +1 using a For loop.
  • Loop from 1 to the number of columns of the box +1 using another Nested For loop.
  • Check if the parent iterator value is equal to 1 or the number of rows of the box using the If statement.
  • Check if the inner loop iterator value is equal to 1 or the number of columns of the box using the If statement.
  • Merge these two conditions in a single If statement using or operator.
  • If the condition is true then print the given character with a space character.
  • Else print space character.
  • Print the newline character after the end of the inner For loop.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of rows of the box as user input using int(input()) and store it in a variable.
  • Give the number of columns of the box as user input using int(input()) and store it in another variable.
  • Give the Character as user input using input() and store it in another variable.

Below is the implementation:

# Give the number of rows and number of columns of the box as user input and store them in two separate variables.
boxrows = int(input("Enter some random number of rows of the box = "))
boxcolumns = int(input("Enter some random number of columns of the box = "))
# Give the character to print as a box pattern as user input and store it in a variable.
userchar = input('Enter some random character to print = ')
# Loop from 1 to the number of rows of the box +1 using a For loop.
for m in range(1, boxrows+1):
    # Loop from 1 to the number of columns of the box +1 using another Nested For loop.
    for n in range(1, boxcolumns+1):
        '''Check if the parent iterator value is equal to 1 or the number of rows of the box using the If statement.
            Check if the inner loop iterator value is equal to 1 or the number of columns of the box using the If statement.
            Merge these two conditions in a single If statement using or operator.       
                  '''
        if(m == 1 or m == boxrows or n == 1 or n == boxcolumns):
            # If the condition is true then print the given character with a space character.
            print(userchar, end=" ")
        else:
            # Else print space character.
            print(" ", end=" ")
    # Print the newline character after the end of the inner For loop.
    print()

Output:

Enter some random number of rows of the box = 9
Enter some random number of columns of the box = 13
Enter some random character to print = &
& & & & & & & & & & & & & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
& & & & & & & & & & & & &

2) C++ Implementation

  • Give the number of rows of the box as user input using cin and store it in a variable.
  • Give the number of columns of the box as user input using cin and store it in another 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 box as user input
    // using cin and store it in a variable.
    // Give the number of columns of the box as user input
    // using cin and store it in another variable.
    int boxrows;
    int boxcolumns;
    cin >> boxrows;
    cin >> boxcolumns;
    // Give the Character as user input using cin and store
    // it in another variable.
    char userchar;
    cin >> userchar;
    cout << endl;
    // Loop from 1 to the number of rows of the box +1 using
    // a For loop.
    for (int m = 1; m <= boxrows; m++) {
        // Loop from 1 to the number of columns of the box
        // +1 using another Nested For loop.
        for (int n = 1; n <= boxcolumns; n++) {
            /*Check if the parent iterator value is equal to
               1 or the number of rows of the box using the
               If statement. Check if the inner loop
               iterator value is equal to 1 or the number of
               columns of the box using the If statement.
                Merge these two conditions in a single If
               statement using or operator. */
            if (m == 1 || m == boxrows || n == 1
                || n == boxcolumns) {
                // If the condition is true then print user
                // character with a space character.
                cout << userchar << " ";
            }
            else {
                // Else print space character.
                cout << "  ";
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the box = 9
Enter some random number of columns of the box = 13
Enter some random character to print = &
& & & & & & & & & & & & & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
& & & & & & & & & & & & &

3) C Implementation

  • Give the number of rows of the box as user input using scanf and store it in a variable.
  • Give the number of columns of the box as user input using scanf and store it in another 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 box as user input
    // using scanf and store it in a variable.
    // Give the number of columns of the box as user input
    // using scanf and store it in another variable.
    int boxrows;
    int boxcolumns;
    // Give the Character as user input using cin and store
    // it in another variable.
    char userchar;
    scanf("%d%d%c",&boxrows,&boxcolumns,&userchar);
    printf("\n");
    // Loop from 1 to the number of rows of the box +1 using
    // a For loop.
    for (int m = 1; m <= boxrows; m++) {
        // Loop from 1 to the number of columns of the box
        // +1 using another Nested For loop.
        for (int n = 1; n <= boxcolumns; n++) {
            /*Check if the parent iterator value is equal to
               1 or the number of rows of the box using the
               If statement. Check if the inner loop
               iterator value is equal to 1 or the number of
               columns of the box using the If statement.
                Merge these two conditions in a single If
               statement using or operator. */
            if (m == 1 || m == boxrows || n == 1
                || n == boxcolumns) {
                // If the condition is true then print user
                // character with a space character.
                printf("%c ", userchar);
            }
            else {
                // Else print space character.
                printf("  ");
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        printf("\n");
    }
    return 0;
}

Output:

Enter some random number of rows of the box = 9
Enter some random number of columns of the box = 13
Enter some random character to print = &
& & & & & & & & & & & & & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
& & & & & & & & & & & & &

Related Programs:

 

Python Program to Print Hollow Box Pattern of Numbers Read More »

Python Program to Print Hollow Right Triangle 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 right-angled Hollow triangle star pattern in C, C++, and python.

Examples:

Example1:

Input:

given number of rows of the hollow right-angled triangle star pattern=11

Output:

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

Example2:

Input:

given number of rows of the hollow right-angled triangle star pattern=8
given character to print =^

Output

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

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

Below are the ways to print Hollow 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 right-angled triangle star 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 first loop iterator value using another Nested For loop.
  • If you closely examine the pattern, you will notice that the star is available on the first or last column or row. So, for the first or last column or row, print a star, otherwise, print space.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the hollow right-angled triangle star pattern
# as static input and store it in a variable.
trianglerows = 7
# Loop from 1 to the number of rows using For loop.
for m in range(1, trianglerows+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # If you closely examine the pattern, you will notice that the
        # star is available on the first or last column or row.
        # So, for the first or last column or row, print a star, otherwise, print space.
        if(m == 1 or m == trianglerows or n == 1 or n == m):
           print('*', end=' ')
        else:
           print(' ', end=' ')
    # Print the newline character after ending of 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 right-angled triangle
    // star pattern as static input and store it in a
    // variable.
    int trianglerows = 11;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= trianglerows;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // If you closely examine the pattern, you will
            // notice that the
            // star is available on the first or last column
            // or row.
            // So, for the first or last column or row,
            // print a star, otherwise, print space.
            if (m == 1 || m == trianglerows || n == 1
                || n == m)
                cout << "* ";
            else
                cout << "  ";
        }
        // Print the newline character after ending of 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-angled triangle
    // star pattern as static input and store it in a
    // variable.
    int trianglerows = 7;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= trianglerows;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // If you closely examine the pattern, you will
            // notice that the
            // star is available on the first or last column
            // or row.
            // So, for the first or last column or row,
            // print a star, otherwise, print space.
            if (m == 1 || m == trianglerows || n == 1
                || n == m)
                printf("* ");
            else
                printf("  ");
        }
        // Print the newline character after ending of inner
        // For loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For loop (User Character )

Approach:

  • Give the number of rows of the right-angled triangle star pattern as user input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • If you closely examine the pattern, you will notice that the star is available on the first or last column or row. So, for the first or last column or row, print a star, otherwise, print space.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

1) Python Implementation

  • Give the number of rows of the hollow right-angled 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-angled triangle as user input using int(input()) and store it in a variable.
trianglerows = int(
    input('Enter some random number of rows of the right-angled triangle = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
for m in range(1, trianglerows+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
         # If you closely examine the pattern, you will notice that the
        # star is available on the first or last column or row.
        # So, for the first or last column or row, print a star, otherwise, print space.
        if(m == 1 or m == trianglerows or n == 1 or n == m):
            print(givencharacter, end=' ')
        else:
            print(' ', end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output

Enter some random number of rows of the right-angled triangle = 8
Enter some random character = ^
^ 
^  ^ 
^      ^ 
^          ^ 
^             ^ 
^                ^ 
^                   ^ 
^ ^ ^ ^ ^ ^ ^ ^

2) C++ Implementation

  • Give the number of rows of the right-angled 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()
{
    int trianglerows;
    char givencharacter;

    // Give the number of rows of the right-angled triangle
    // as user input using cin and store it in a variable.
    cout << "Enter some random number of rows of the "
            "right-angled triangle = "
         << endl;
    cin >> trianglerows;
    // 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 <= trianglerows;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // If you closely examine the pattern, you will
            // notice that the
            // star is available on the first or last column
            // or row.
            // So, for the first or last column or row,
            // print a star, otherwise, print space.
            if (m == 1 || m == trianglerows || n == 1
                || n == m)
                cout << givencharacter << " ";
            else
                cout << "  ";
        }
        // Print the newline character after ending of inner
        // For loop.
        cout << endl;
    }
    return 0;
}

Output

Enter some random number of rows of the right-angled triangle = 8
Enter some random character = ^
^ 
^  ^ 
^      ^ 
^          ^ 
^             ^ 
^                ^ 
^                   ^ 
^ ^ ^ ^ ^ ^ ^ ^

3) C Implementation

  • Give the number of rows of the right-angled 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()
{

    int trianglerows;
    char givencharacter;
    // Give the number of rows of the right-angled triangle
    // as user input using cin and store it in a variable.
    scanf("%d", &trianglerows);

    // Give the Character as user input using cin and store
    // it in another variable.
    scanf("%c", &givencharacter);
    printf("\n");
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= trianglerows;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // If you closely examine the pattern, you will
            // notice that the
            // star is available on the first or last column
            // or row.
            // So, for the first or last column or row,
            // print a star, otherwise, print space.
            if (m == 1 || m == trianglerows || n == 1
                || n == m)
                printf("%c ", givencharacter);
            else
                printf("  ");
        }
        // Print the newline character after ending of inner
        // For loop.
        printf("\n");
    }
    return 0;
}

Output

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

Related Programs:

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

Python Program to Print Plus Star Pattern

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, the task is to print plus star Patterns in C, C++, and Python.

Examples:

Example1:

Input:

given number of rows = 9

Output:

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

Example2:

Input:

given number of rows =5
given character to print ='<'

Output:

5<
              < 
              < 
              < 
              < 
< < < < < < < < < 
              < 
              < 
              < 
              <

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

Below are the ways to Print Plus Star Pattern 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.
  • Loop from 1 to 2*number of rows using For loop.
  • Loop from 1 to 2*number of rows using another For loop(Nested For loop).
  • Check if the parent loop iterator value is equal to the number of rows using the If statement.
  • Check if the inner loop iterator value is equal to the number of rows using the If statement.
  • Merge these both statements using or operator.
  • If this statement is true then print star character.
  • Else print space character.
  • Print the newline Character after the end of the inner for loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numberows = 9
# Loop from 1 to 2*number of rows using For loop.
for m in range(1, 2*numberows):
    # Loop from 1 to 2*number of rows using another For loop(Nested For loop).
    for n in range(1, 2*numberows):
        '''Check if the parent loop iterator value is equal to the number of rows using the If statement.
        Check if the inner loop iterator value is equal to the number of rows using the If statement.
        Merge these both statements using or operator.'''
        if(m == numberows or n == numberows):
            # If this statement is true then print star character.
            print('*', end=' ')
        else:
            # Else print space character.
            print(' ', end=' ')

    # Print the newline character after the end of the inner For loop.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

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

    // # Loop from 1 to 2*number of rows using For loop.
    for (int m = 1; m < 2 * numberows; m++) {
        // Loop from 1 to 2*number of rows using another For
        // loop(Nested For loop).
        for (int n = 1; n < 2 * numberows; n++) {
            /*Check if the parent loop iterator value is
         equal to the number of rows using the If statement.
         Check if the inner loop iterator value is equal to
         the number of rows using the If statement. Merge
         these both statements using or operator.*/
            if (m == numberows || n == numberows)
                // If this statement is true then print star
                // character.
                cout << "* ";
            else
                // Else print space character.
                cout << "  ";
        }
        // Print the newline character after the end of
        // the inner For loop.
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

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

    // # Loop from 1 to 2*number of rows using For loop.
    for (int m = 1; m < 2 * numberows; m++) {
        // Loop from 1 to 2*number of rows using another For
        // loop(Nested For loop).
        for (int n = 1; n < 2 * numberows; n++) {
            /*Check if the parent loop iterator value is
         equal to the number of rows using the If statement.
         Check if the inner loop iterator value is equal to
         the number of rows using the If statement. Merge
         these both statements using or operator.*/
            if (m == numberows || n == numberows)
                // If this statement is true then print star
                // character.
                printf("* ");
            else
                // Else print space character.
                printf("  ");
        }
        // Print the newline character after the end of
        // the inner For loop.
        printf("\n");
    }

    return 0;
}

Output:

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

Method #2: Using For Loop (User Character)

Approach:

  • Give the number of rows as user input and store it in a variable.
  • Give the character as user input and store it in a variable.
  • Loop from 1 to 2*number of rows using For loop.
  • Loop from 1 to 2*number of rows using another For loop(Nested For loop).
  • Check if the parent loop iterator value is equal to the number of rows using the If statement.
  • Check if the inner loop iterator value is equal to the number of rows using the If statement.
  • Merge these both statements using or operator.
  • If this statement is true then print the given character.
  • Else print space character.
  • Print the newline Character after the end of the inner for loop.
  • The Exit of the Program.

1) Python Implementation

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

Below is the implementation:

# Give the word as user input using int(input()) and store it in a variable.
numberows = 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 2*number of rows using For loop.
for m in range(1, 2*numberows):
    # Loop from 1 to 2*number of rows using another For loop(Nested For loop).
    for n in range(1, 2*numberows):
        '''Check if the parent loop iterator value is equal to the number of rows using the If statement.
        Check if the inner loop iterator value is equal to the number of rows using the If statement.
        Merge these both statements using or operator.'''
        if(m == numberows or n == numberows):
            # If this statement is true then print givencharacter
            print(givencharacter, end=' ')
        else:
            # Else print space character.
            print(' ', end=' ')

    # Print the newline character after the end of the inner For loop.
    print()

Output:

Enter some random number of rows = 5
Enter some random character = <
              < 
              < 
              < 
              < 
< < < < < < < < < 
              < 
              < 
              < 
              <

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 cin and store it in a
    // variable.
    int numberows;
    char givencharacter;
    cout << "Enter some random number of rows = " << endl;
    cin >> numberows;
    // 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 2*number of rows using For loop.
    for (int m = 1; m < 2 * numberows; m++) {
        // Loop from 1 to 2*number of rows using another For
        // loop(Nested For loop).
        for (int n = 1; n < 2 * numberows; n++) {
            /*Check if the parent loop iterator value is
         equal to the number of rows using the If statement.
         Check if the inner loop iterator value is equal to
         the number of rows using the If statement. Merge
         these both statements using or operator.*/
            if (m == numberows || n == numberows)
                // If this statement is true then print
                // givencharacter

                cout << givencharacter << " ";
            else
                // Else print space character.
                cout << "  ";
        }
        // Print the newline character after the end of
        // the inner For loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows = 5
Enter some random character = <
              < 
              < 
              < 
              < 
< < < < < < < < < 
              < 
              < 
              < 
              <

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 numberows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &numberows);
    scanf("%c", &givencharacter);
    printf("\n");

    // # Loop from 1 to 2*number of rows using For loop.
    for (int m = 1; m < 2 * numberows; m++) {
        // Loop from 1 to 2*number of rows using another For
        // loop(Nested For loop).
        for (int n = 1; n < 2 * numberows; n++) {
            /*Check if the parent loop iterator value is
         equal to the number of rows using the If statement.
         Check if the inner loop iterator value is equal to
         the number of rows using the If statement. Merge
         these both statements using or operator.*/
            if (m == numberows || n == numberows)
                // If this statement is true then print
                // givencharacter
                printf("%c ", givencharacter);
            else
                // Else print space character.
                printf("  ");
        }
        // Print the newline character after the end of
        // the inner For loop.
        printf("\n");
    }

    return 0;
}

Output:

5<
              < 
              < 
              < 
              < 
< < < < < < < < < 
              < 
              < 
              < 
              <

Related Programs:

Python Program to Print Plus Star Pattern Read More »

Python Program to Print Exponentially Increasing 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 number of rows, the task is to print Exponentially Increasing Star Pattern in C, C++, and Python

Examples:

Example1:

Input:

given number of rows =5

Output:

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

Example2:

Input:

given number of rows =4
given character to print =@

Output:

@ 
@ @ 
@ @ @ @ 
@ @ @ @ @ @ @ @ 
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @

Program to Print Exponentially Increasing Star Pattern in C, C++, and Python

Below are the ways to print Exponentially Increasing Star Pattern in C, C++, and python.

Method #1: Using For loop (Star Character)

Approach:

  • Give the number of rows of the pattern as static input and store it in a variable.
  • Loop till a given number of rows using For loop.
  • Calculate the exponential value of the first loop iterator value using 2**(iterator value of the first loop).
  • Loop till the exponential value using another For loop(Nested For loop).
  • Print the star character in the inner for loop.
  • Print the newline character after the end of the inner for loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the pattern as static input and store it in a variable.
rowsnumb = 5
# Loop till a given number of rows using For loop.
for m in range(rowsnumb+1):
  # Calculate the exponential value of the first loop iterator
  # value using 2**(iterator value of the first loop).
    expvalue = 2**m
    # Loop till the exponential value using another For loop(Nested For loop).
    for n in range(expvalue):
      # Print the star character in the inner for loop.
        print("*", end=" ")
    # Print the newline character after the end of the inner for loop.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    // Give the number of rows of the pattern as static
    // input and store it in a variable.
    int rowsnumb = 5;
    // Loop till a given number of rows using For loop.
    for (int m = 0; m <= rowsnumb; m++) {
        // Calculate the exponential value of the first loop
        // iterator value using 2**(iterator value of the
        // first loop).
        int expvalue = pow(2, m);
        // Loop till the exponential value using another For
        // loop(Nested For loop).
        for (int n = 1; n <= expvalue; n++) {
            // Print the star character in the inner for
            // loop.
            cout << "* ";
        }
        // Print the newline character after the end of the
        // inner for loop.
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <math.h>
#include <stdio.h>
int main()
{
    // Give the number of rows of the pattern as static
    // input and store it in a variable.
    int rowsnumb = 5;
    // Loop till a given number of rows using For loop.
    for (int m = 0; m <= rowsnumb; m++) {
        // Calculate the exponential value of the first loop
        // iterator value using 2**(iterator value of the
        // first loop).
        int expvalue = pow(2, m);
        // Loop till the exponential value using another For
        // loop(Nested For loop).
        for (int n = 1; n <= expvalue; n++) {
            // Print the star character in the inner for
            // loop.
            printf("* ");
        }
        // Print the newline character after the end of the
        // inner for loop.
        printf("\n");
    }

    return 0;
}

Output:

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

Method #2: Using For loop (User Character)

Approach:

  • Give the number of rows of the 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 till a given number of rows using For loop.
  • Calculate the exponential value of the first loop iterator value using 2**(iterator value of the first loop).
  • Loop till the exponential value using another For loop(Nested For loop).
  • Print the user character in the inner for loop.
  • Print the newline character after the end of the inner for loop.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of rows 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 using int(input()) and store it in a variable.
rowsnumb = int(input('Enter some random number of rows = '))
# Give the Character as user input using input() and store it in another variable.
usercharact = input('Enter some random character to print = ')
for m in range(rowsnumb+1):
  # Calculate the exponential value of the first loop iterator
  # value using 2**(iterator value of the first loop).
    expvalue = 2**m
    # Loop till the exponential value using another For loop(Nested For loop).
    for n in range(expvalue):
      # Print the user character in the inner for loop.
        print(usercharact, end=" ")
    # Print the newline character after the end of the inner for loop.
    print()

Output:

Enter some random number of rows = 4
Enter some random character to print = @
@ 
@ @ 
@ @ @ @ 
@ @ @ @ @ @ @ @ 
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @

2) C++ Implementation

  • Give the number of rows 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>
#include <math.h>
using namespace std;

int main()
{
    // Give the number of rows of the pattern as user input
    // and store it in a variable.
    int rowsnumb;
    cin >> rowsnumb;
    // Give the character to print as user input and store
    // it in a variable.
    char usercharact;
    cin >> usercharact;
    cout<<endl;
    // Loop till a given number of rows using For loop.
    for (int m = 0; m <= rowsnumb; m++) {
        // Calculate the exponential value of the first loop
        // iterator value using 2**(iterator value of the
        // first loop).
        int expvalue = pow(2, m);
        // Loop till the exponential value using another For
        // loop(Nested For loop).
        for (int n = 1; n <= expvalue; n++) {
            // Print the user character in the inner for
            // loop.
            cout << usercharact << " ";
        }
        // Print the newline character after the end of the
        // inner for loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows = 4
Enter some random character to print = @
@ 
@ @ 
@ @ @ @ 
@ @ @ @ @ @ @ @ 
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @

3) C Implementation

  • Give the number of rows 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 <math.h>
#include <stdio.h>
int main()
{ // Give the number of rows of the pattern as user input
    // and store it in a variable.
    int rowsnumb;
    scanf("%d", &rowsnumb);
    // Give the character to print as user input and store
    // it in a variable.
    char usercharact;
    scanf("%c", &usercharact);
    // Loop till a given number of rows using For loop.
    for (int m = 0; m <= rowsnumb; m++) {
        // Calculate the exponential value of the first loop
        // iterator value using 2**(iterator value of the
        // first loop).
        int expvalue = pow(2, m);
        // Loop till the exponential value using another For
        // loop(Nested For loop).
        for (int n = 1; n <= expvalue; n++) {
            // Print the user character in the inner for
            // loop.
            printf("%c ", usercharact);
        }
        // Print the newline character after the end of the
        // inner for loop.
        printf("\n");
    }

    return 0;
}

Output:

@ 
@ @ 
@ @ @ @ 
@ @ @ @ @ @ @ @ 
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @

Related Programs:

Python Program to Print Exponentially Increasing Star Pattern Read More »

Python Program to Print Mirrored Half Diamond 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 diamond pattern, the task is to print the Mirrored Half diamond Pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows =9

Output:

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

Example2:

Input:

Given number of rows =7
Given Character to print ='+'

Output:

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

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

Below are the ways to print Mirrored 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 patterns as static input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Loop from 0 to the number of rows -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 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.
  • Loop from the number of rows to 0 in decreasing order using For loop.
  • Loop from 0 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 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 = 9
# Loop from 0 to the number of rows using For Loop.
for m in range(0, diamondrows):
    # Loop from 0 to the number of rows -iterator value of the parent
    # For loop using another For loop(Nested For loop).
    for n in range(0, diamondrows - m):
        # Print the space character in the inner For loop.
        print(' ', end='')
    # Loop from 0 to the iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(0, m):
        # Print the star character
        print('*', end='')
    # After the end of the inner for Loops print the Newline Character.
    print()
# Loop from number of rows to 0 in decreasing order using For loop.
for m in range(diamondrows, 0, -1):
    # Loop from 0 to the number of rows -iterator value of the parent
    # For loop using another For loop(Nested For loop).
    for n in range(0, diamondrows - m):
        # Print the space character in the inner For loop.
        print(' ', end='')
    # Loop from 0 to the iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(0, 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 = 9;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < diamondrows; m++) {

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

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

            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < 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 = 9;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < diamondrows; m++) {

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

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

            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < 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 number of diamond patterns as static input and store it in a variable.
  • Give the character as user input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Loop from 0 to the number of rows -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 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.
  • Loop from the number of rows to 0 in decreasing order using For loop.
  • Loop from 0 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 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

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

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

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

            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < m; l++) {
            // Print the givencharacter
            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);
    printf("\n");
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < diamondrows; m++) {

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

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

            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another For loop(Nested For loop).
        for (int l = 0; l < 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 Mirrored Half Diamond Star Pattern Read More »

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

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language

Given the number of rows of the diamond pattern, the task is to print the 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:

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

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

Below are the ways to print 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).
  • In the inner for loop Print the star character with space.
  • 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).
  • In the inner for loop Print the star character with space.
  • 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):
        # In the inner for loop Print the star character with space.
        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):
        # In the inner for loop Print the star character with space.
        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++) {

            // In the inner for loop Print the star
            // character with space.
            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++) {

            // In the inner for loop Print the star
            // character with space.
            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++) {

            // In the inner for loop Print the star
            // character with space.
            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++) {

            // In the inner for loop Print the star
            // character with space.
            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.
  • Loop from 0 to the number of rows using For loop.
  • Loop till the first iterator value using another For loop(Nested For loop).
  • In the inner for loop Print the star character with space.
  • 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).
  • In the inner for loop Print the star character with space.
  • 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):
        # In the inner for loop Print the star character with space.
        print(givencharacter, 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):
        # In the inner for loop Print the star character with space.
        print(givencharacter, 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++) {

            // In the inner for loop Print the star
            // character with space.
            cout << givencharacter << " ";
        }
        // 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++) {

            // In the inner for loop Print the star
            // character with space.
            cout << givencharacter << " ";
        }
        // 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++) {

            // In the inner for loop Print the star
            // character with space.
            printf("%c ", givencharacter);
        }
        // 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++) {

            // In the inner for loop Print the star
            // character with space.
            printf("%c ", givencharacter);
        }
        // After the end of the inner for loop print the
        // Newline Character.
        printf("\n");
    }
    return 0;
}

Output:

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

Related Programs:

Program to Print Half Diamond Star Pattern in C,C++, and Python Read More »

Python Program to Print Pyramid 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 pyramid, the task is to print the Pyramid Star pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows of the pyramid  =10

Output:

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

Example2:

Input:

Given number of rows of the pyramid  =10
Given character to print='^'

Output:

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

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

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

Method #1: Using For Loop (Star Character)

Approach:

  • Give the number of rows of the pyramid as static input and store it in a variable.
  • Loop from 0 to the number of rows using For Loop.
  • Loop from 0 to the number of rows – iterator value-1 of the parent For loop using another Nested For loop(Inner For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the iterator value+1 of the parent For loop using another Nested For loop(Inner For loop).
  • Print the star character with a space character in the inner For loop.
  • After the end of the two 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 pyramid as static input and store it in a variable.
pyRows = 10
# Loop from the number of rows to 0 in decreasing order using For Loop.
# Loop from 0 to the number of rows using For Loop.
for m in range(0, pyRows):
    # Loop from 0 to the number of rows - iterator value-1 of the parent For loop\
    # using another Nested For loop(Inner For loop).
    for n in range(0, pyRows-m-1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 0 to the iterator value+1 of the parent For loop
    # using another Nested For loop(Inner For loop).

    for l in range(0, m+1):
        # Print the star character with a star character in the inner For loop.
        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 pyramid as static
    // input and store it in a variable.
    int pyRows = 10;
    // Loop from 0 to the number of rows using For Loop.
    for (int m = 0; m < pyRows; m++) {
        // Loop from 0 to the number of rows - iterator value-1 of the parent For loop\
    // using another Nested For loop(Inner For loop).
        for (int n = 0; n < pyRows - m - 1; n++) {
            //  Print the space character in the inner For
            //  loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value+1 of the parent
        // For loop
        // using another Nested For loop(Inner For loop).
        for (int l = 0; l < m + 1; l++) {
            // Print the star character with a space
            // character in the inner For loop.
            cout << "* ";
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        cout << endl;
    }
    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the pyramid as static
    // input and store it in a variable.
    int pyRows = 10;
    // Loop from 0 to the number of rows using For Loop.
    for (int m = 0; m < pyRows; m++) {
        // Loop from 0 to the number of rows - iterator value-1 of the parent For loop\
    // using another Nested For loop(Inner For loop).
        for (int n = 0; n < pyRows - m - 1; n++) {
            //  Print the space character in the inner For
            //  loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value+1 of the parent
        // For loop
        // using another Nested For loop(Inner For loop).
        for (int l = 0; l < m + 1; l++) {
            // Print the star character with a space
            // character in the inner For loop.
            printf("* ");
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For Loop (User Character)

Approach:

  • Give the number of rows of the pyramid as user input and store it in a variable.
  • Give the character as static input and store it in a variable.
  • Loop from the number of rows to 0 in decreasing order using For Loop.
  • Loop from 0 to the number of rows – iterator value of the parent For loop using another Nested For loop(Inner For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the iterator value of the parent For loop using another Nested For loop(Inner For loop).
  • Print the given character with a space character in the inner For loop.
  • After the end of the two inner For loops print the newline character.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of rows of the pyramid 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 pyramid as user input using int(input()) and store it in a variable.
pyRows = int(input(
    'Enter some random number of rows of pyramid = '))
# 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, pyRows):
    # Loop from 0 to the number of rows - iterator value-1 of the parent For loop\
    # using another Nested For loop(Inner For loop).
    for n in range(0, pyRows-m-1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 0 to the iterator value+1 of the parent For loop
    # using another Nested For loop(Inner For loop).

    for l in range(0, m+1):
        # Print the given character with a space character in the inner For loop.
        print(givencharacter, end=' ')
    print()

Output:

Enter some random number of rows of pyramid = 10 
Enter some random character = ^
                 ^ 
               ^ ^ 
             ^ ^ ^ 
           ^ ^ ^ ^ 
          ^ ^ ^ ^ ^ 
        ^ ^ ^ ^ ^ ^ 
      ^ ^ ^ ^ ^ ^ ^ 
    ^ ^ ^ ^ ^ ^ ^ ^ 
  ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^

2) C++ Implementation

  • Give the number of rows of the pyramid as user input using scanf 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 pyRows;
    char givencharacter;
    cout << "Enter some random number of rows = " << endl;
    cin >> pyRows;
    // 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 < pyRows; m++) {
        // Loop from 0 to the number of rows - iterator value-1 of the parent For loop\
    // using another Nested For loop(Inner For loop).
        for (int n = 0; n < pyRows - m - 1; n++) {
            //  Print the space character in the inner For
            //  loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value+1 of the parent
        // For loop
        // using another Nested For loop(Inner For loop).
        for (int l = 0; l < m + 1; l++) {
            // Print the given character with a space
            // character in the inner For loop.
            cout <<givencharacter<<" ";
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        cout << endl;
    }
    return 0;
}

Output:

Enter some random number of rows of pyramid = 
10 
Enter some random character = 
^
                 ^ 
               ^ ^ 
             ^ ^ ^ 
           ^ ^ ^ ^ 
          ^ ^ ^ ^ ^ 
        ^ ^ ^ ^ ^ ^ 
      ^ ^ ^ ^ ^ ^ ^ 
    ^ ^ ^ ^ ^ ^ ^ ^ 
  ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^

3) C Implementation

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

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows
    //  as user input using scanf and store it in a
    // variable.
    int pyRows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &pyRows);
    scanf("%c", &givencharacter);
    // Loop from 0 to the number of rows using For Loop.
    for (int m = 0; m < pyRows; m++) {
        // Loop from 0 to the number of rows - iterator value-1 of the parent For loop\
    // using another Nested For loop(Inner For loop).
        for (int n = 0; n < pyRows - m - 1; n++) {
            //  Print the space character in the inner For
            //  loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value+1 of the parent
        // For loop
        // using another Nested For loop(Inner For loop).
        for (int l = 0; l < m + 1; l++) {
            // Print the given  character with a space
            // character in the inner For loop.
            printf("%c ", givencharacter);
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

10^
                 ^ 
               ^ ^ 
             ^ ^ ^ 
           ^ ^ ^ ^ 
          ^ ^ ^ ^ ^ 
        ^ ^ ^ ^ ^ ^ 
      ^ ^ ^ ^ ^ ^ ^ 
    ^ ^ ^ ^ ^ ^ ^ ^ 
  ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^

Related Programs:

Python Program to Print Pyramid Star Pattern Read More »

Program to Count the Number of Blank Spaces in a Text File

Python Program to Count the Number of Blank Spaces in a Text File

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Files in Python:

Python File handling is a method that allows you to save the program’s output to a file or read data from a file. In the programming world, file handling is a critical notion. File management is employed in practically every type of project. For example, suppose you’re developing an inventory management system. You have data connected to sales and purchases in the inventory management system, thus you must save that data somewhere. You can save that data to a file using Python file management. If you want to undertake data analysis, you must be given data in the form of a comma-separated file or a Microsoft Excel file. You can read data from a file and also store output back into it using file handling.

Given the file, the task is to count the number of blank spaces in a Text File.

Program to Count the Number of Blank Spaces in a Text File

Below is the full approach to calculate the total number of Blank Spaces in the given Text File.

Approach:

  • Take a variable say blankCount that stores the calculate the total number of blank spaces in a given file and initialize it to 0.
  • Create the file or upload the existing file.
  • Enter the file name of the file using the input() function and store it in a variable.
  • In read mode, open the file with the entered file name.
  • Using for loop, Traverse the lines in the file.
  • Split the line into words using the split() function.
  • To traverse the words in the list, use a for loop, then another for loop to traverse the letters in the word.
  • Check to see if the character is a space using isspace, and if so, increase the blank count by 1.
  • Print the blankCount.
  • The Exit of the Program

Below is the implementation:

# Take a variable say blankCount that stores the calculate the
# total number of blank spaces in a given file and initialize it to 0.
blankCount = 0
# Enter the file name of the file using the input() function and store it in a variable.
filename = input("Enter the file name = ")
# In read mode, open the file with the entered file name.
with open(filename, 'r') as givenfile:
  # Using for loop, go over the lines in the first file.
    for fileline in givenfile:
        # Split the line into words using the split() function.
        wordslist = fileline.split()
        # To traverse the words in the list, use a for loop, then another
        # for loop to traverse the letters in the word.
        for words in wordslist:
            for char in words:
                # Check to see if the letter is a space using isspace,
                # and if so, increase the blank count by 1.
                if(char.isspace):
                    blankCount = blankCount+1
print('The total count of the blank spaces in the given file = ', blankCount)

Output:

Enter the file name = hello.txt
The total count of the blank spaces in the given file = 21

Explanation:

  • A file name must be entered by the user.
  • In read mode, the file is opened with the open() method.
  • To read through each line of the file, a for loop is utilized.
  • Using split, each line is separated into a list of words ().
  • A for loop is used to go through the list of words, and another for loop is used to go through the letters of the word.
  • If the iterated letter is a space, the letter count is increased.
  • The total number of blank space occurrences is printed.

Google Colab Images:

Files and Code:

Code:

Output Image:

Hello.txt

Related Programs:

 

Python Program to Count the Number of Blank Spaces in a Text File Read More »

Program to Add a Key , Value Pair to the Dictionary

Python Program to Add a Key, Value Pair to the Dictionary

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Dictionaries in Python:

In Python, a dictionary dict is a one-to-one mapping; it includes a set of (key, value) pairs, with each key mapped to a value. It exemplifies a hash map or hash table (from Computer Science).

Each key denotes a value and is separated by a colon (:).

Curly brackets are used to define a dictionary. The value to the left of the colon is known as the key, while the value to the right of the colon is known as the value. A comma separates each (key, value) pair.

Example:

Example1:

Input:

Enter some random key = aplustopper
Enter some random value of integer type = 2451

Output:

The dictionary after adding the key = aplustopper and value element = 2451 is : 
{'aplustopper': 2451}

Example2:

Input:

Enter some random key = 752
Enter some random value of integer type = 251

Output:

The dictionary after adding the key = 752 and value element = 251 is : 
{752: 251}

Example3:

Input:

Enter some random key = 234
Enter some random value of string type = btechgeeks

Output:

The dictionary after adding the key = 234 and value element = btechgeeks is : 
{234: 'btechgeeks'}

Program to Add a Key , Value Pair to the Dictionary

There are several ways to add a key, value pair to the dictionary some of them are:

Method #1:Using update() function (User input)

Approach:

  • Give the key , value as user input and store them in two separate variables.
  • Declare a dictionary and set its initial value to empty using {} or dict().
  • To add the key-value pair to the dictionary, use the update() function.
  • The Modified dictionary is printed.
  • Exit of program.

i)Key of string datatype and value as integer datatype

Below is the implementation:

# given some random key
keyelement = input("Enter some random key = ")
# given some random value of int datatype
valueelement = int(input("Enter some random value of integer type = "))
# Declare a dictionary and set its initial value to empty using {} or dict()
sampledictionary = {}
# To add the key-value pair to the dictionary, use the update() function.
sampledictionary.update({keyelement: valueelement})
# The Modified dictionary is printed
print("The dictionary after adding the key =", keyelement,
      "and value element =", valueelement, " is : ")
print(sampledictionary)

Output:

Enter some random key = btechgeeks
Enter some random value of integer type = 2841
The dictionary after adding the key = btechgeeks and value element = 2841 is : 
{'btechgeeks': 2841}

ii)Key of integer datatype and value as integer datatype

# given some random key
keyelement = int(input("Enter some random key = "))
# given some random value of int datatype
valueelement = int(input("Enter some random value of integer type = "))
# Declare a dictionary and set its initial value to empty using {} or dict()
sampledictionary = {}
# To add the key-value pair to the dictionary, use the update() function.
sampledictionary.update({keyelement: valueelement})
# The Modified dictionary is printed
print("The dictionary after adding the key =", keyelement,
      "and value element =", valueelement, " is : ")
print(sampledictionary)

Output:

Enter some random key = 752
Enter some random value of integer type = 251
The dictionary after adding the key = 752 and value element = 251 is : 
{752: 251}

iii)Key of integer datatype and value as string datatype

# given some random key
keyelement = int(input("Enter some random key = "))
# given some random value of string datatype
valueelement = input("Enter some random value of string type = ")
# Declare a dictionary and set its initial value to empty using {} or dict()
sampledictionary = {}
# To add the key-value pair to the dictionary, use the update() function.
sampledictionary.update({keyelement: valueelement})
# The Modified dictionary is printed
print("The dictionary after adding the key =", keyelement,
      "and value element =", valueelement, " is : ")
print(sampledictionary)

Output:

Enter some random key = 234
Enter some random value of string type = btechgeeks
The dictionary after adding the key = 234 and value element = btechgeeks is : 
{234: 'btechgeeks'}

Method #2:Using [] operator(User input)

Below is the implementation:

# given some random key
keyelement = input("Enter some random key = ")
# given some random value of integer datatype
valueelement = int(input("Enter some random value of integer type = "))
# Declare a dictionary and set its initial value to empty using {} or dict()
sampledictionary = {}
# using [] function
sampledictionary[keyelement] = valueelement
# The Modified dictionary is printed
print("The dictionary after adding the key =", keyelement,
      "and value element =", valueelement, " is : ")
print(sampledictionary)

Output:

Enter some random key = aplustopper
Enter some random value of integer type = 2451
The dictionary after adding the key = aplustopper and value element = 2451 is : 
{'aplustopper': 2451}

Related Programs:

Python Program to Add a Key, Value Pair to the Dictionary Read More »

Program to Count the Occurrences of a Word in a Text File

Python Program to Count the Occurrences of a Word in a Text File

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Files and File Handling in Python:

A file is a piece of information or data that is saved in computer storage devices. One of the most crucial aspects of any language is file handling. The Python programming language allows two types of files. The first is a text file, which stores data in the form of text that humans and computers can read. The second type is a binary file, which stores binary data and is only readable by a computer. I

File handling is the process of managing files on a file system. Each operating system has its own method of storing files.

Python file handling comes in useful when working with files in our projects. We don’t need to be concerned about the underlying operating system or its file system rules and operations.

Program to Count the Occurrences of a Word in a Text File in Python

Below is the full approach to calculate the number of occurrences of the given word in the given text file in python

Approach:

  • Take a variable to say wordCount that stores the count of the given word in a given file and initializes it to 0.
  • Scan the given word by user input using the int(input()) function and store it in a variable.
  • Create the file or upload the existing file.
  • Enter the file’s name into the input() function and save it to a variable.
  • Open the file with the entered file name in read mode.
  • Traverse the lines in the file using a For loop.
  • Using the split() method, split the line into words.
  • To traverse the words in the list, use a For loop.
  • If the iterator value is equal to the given word then increase the wordCount by 1.
  • Print the wordCount of the given word.
  • Exit of Program

Below is the implementation:

# Take a variable say wordCount that stores the calculate the
# count of given word in a given file and initialize it to 0.
wordCount = 0
# Scan the given word by user input using the int(input()) function and store it in a variable.
givenword = input('Enter the given word = ')
# Enter the file's name into the input() function and save it to a variable.
filename = input("Enter the file name = ")
# Open the file with the entered file name in read mode.
with open(filename, 'r') as givenfile:
  # Traverse the lines in the file using a For loop.
    for fileline in givenfile:
        # Using the split() method, split the line into words.
        wordslist = fileline.split()
        # To traverse the words in the list, use a For loop.
        for words in wordslist:
            # If the iterator value is equal to the given word then increase the wordCount by 1.
            if(words == givenword):
                wordCount = wordCount+1
# Print the wordCount.
print('The total count of the given word {',
      givenword, '} in the given file = ', wordCount)

Output:

Enter the given word = btechgeeks Enter the file name = hello.txt 
The total count of the given word { btechgeeks } in the given file = 3

Explanation:

  • A file name and the word to be searched must be entered by the user.
  • In read mode, the file is opened with the open() method.
  • To read through each line of the file, a for loop is utilized.
  • Using split(), each line is divided into a list of words.
  • Another for loop is used to scan the list, and each word in the list is compared to the word given by the user.
  • If both words are the same, the word count is increased.
  • The total number of instances of the word is displayed.
  • Exit of the program.

hello.txt

hello this is btechgeeks python programming learning platform btechgeeks btechgeeks btechgeeks.

Google Colab Images:

Files and Code:

Code:

Output Image:

Hello.txt


Related Programs:

Python Program to Count the Occurrences of a Word in a Text File Read More »