Python Pandas Period.day Attribute

Python Pandas Period.day Attribute

Python is an excellent language for data analysis, thanks to its vast ecosystem of data-centric Python packages. One of these packages is Pandas, which makes importing and analysing data a lot easier.

Pandas

Pandas is a Python open-source library that provides high-performance data manipulation. Pandas gets its name from the term Panel Data, which means Econometrics from Multidimensional data. Wes McKinney created it in 2008 for data analysis in Python.

Data analysis necessitates a significant amount of processing, such as restructuring, cleaning, or merging. Numpy, Scipy, Cython, and Panda are some tools for fast data processing. But we prefer Pandas because they are faster, simpler, and more expressive than other tools.

Pandas is built on top of the Numpy package, so Numpy is required to run Pandas.

Period in Pandas:

The Time Periods represent the length of time, such as days, years, quarters, and months. It’s a class that lets us convert frequencies to periods.

Period.day Pandas:

For the given Period object, the day attribute returns the month’s day. It returns the  integer value.

Syntax :

Period.day

Parameters:

It has no arguments/parameters.

Return :

It returns the day number in the corresponding month and year.

Pandas Period.day Attribute in Python

We can also pass freq = H( hourly frequency), T(minutely), S(secondly), D(Day), Q(quaterly) etc.

Example1

To find the day of the month in the given object, we’ll use the Period. day attribute.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random frequency(here D=Day), year, month, days as the arguments to the Period() function of the pandas module to get the Period object
  • Print the given period object
  • Apply the day attribute on the above period object to get the start time of the given period object.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random frequency(here D=Day), year, month, day,
# as the arguments to the Period() function
# of the pandas module to get the Period object
period_obj = pd.Period(freq ='D', year = 2020, month = 9, day = 28)
  
# Print the given period object
print("The given period object:")
print(period_obj)
print()
# Apply day() attribute on the above period object to get the 
# day value in the above period object
print("The day value of the given period object:")
print(period_obj.day)

Output:

The given period object:
2020-09-28

The day value of the given period object:
28

Explanation:

The Period. day attribute returned 28 in the output, which is the value of the day in the given period object.

Example2

To find the day value by passing the quarter value, we’ll use the Period. day attribute.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random frequency(here Q=quaterly), year, quarter as the arguments to the Period() function of the pandas module to get the Period object
  • Print the given period object
  • Apply day() attribute on the above period object to get the day value in the quarter.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random frequency(here Q=quaterly), year, quarter
# as the arguments to the Period() function
# of the pandas module to get the Period object
period_obj = pd.Period(freq ='Q', year = 2021,quarter =2)
  
# Print the given period object
print("The given period object:")
print(period_obj)
print()
# Apply day() attribute on the above period object to get the 
# day value in the quarter
print("The day value of the given quarter:")
print(period_obj.day)

Output:

The given period object:
2021Q2

The day value of the given quarter:
30

Explanation:

Here it prints the last day of the second quarter.

Python Program to Find the Product of two Numbers Using Recursion

Program to Find the Product of two Numbers Using Recursion

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Recursion in Python:

When a function calls itself and loops until it reaches the intended end state, this is referred to as recursion. It is based on the mathematics idea of recursive definitions, which define elements in a set in terms of other members in the set.

Each recursive implementation contains a base case in which the desired state is reached, and a recursive case in which the desired state is not reached and the function enters another recursive phase.

On each step, the behavior in the recursive situation before the recursive function call, the internal self-call, is repeated. Recursive structures are beneficial when a larger problem (the base case) can be solved by solving repeated subproblems (the recursive case) that incrementally advance the program to the base case.
It behaves similarly to for and while loops, with the exception that recursion moves closer to the desired condition, whereas for loops run a defined number of times and while loops run until the condition is no longer met.

In other words, recursion is declarative because you specify the desired state, whereas for/while loops are iterative because you provide the number of repeats.

Given two numbers the task is to find the product of the given two numbers using recursion in Python.

Examples:

Example1:

Input:

given the first number = 27 
given the second number = 19

Output:

The product of the given numbers 27 and 19 is 27 * 19 = 513

Example2:

Input:

given the first number = 23
given the second number = 38

Output:

The product of the given numbers 23 and 38 is 23 * 38 = 874

Program to Find the Product of two Numbers Using Recursion

Below are the ways to Find the Product of two Numbers using the recursive approach in Python:

1)Using Recursion(Static Input)

Approach:

  • Give the two numbers as static input and store them as two variables.
  • To find the product of the two numbers, pass the numbers as arguments to a recursive function.
  • Give the base condition that if the first number is less than the second, execute the method recursively with the numbers swapped.
  • If the second number is not equal to 0, call the method again recursively otherwise, return 0.
  • The function returns the product of the given two numbers.
  • Print the result using the print() function.
  • The exit of the program.

Below is the implementation:

# function who calculates the product of the given two numbers recursively
def productRecursion(firstnumb, secondnumb):
  # Give the base condition that if the first number is less than the second,
  # execute the method recursively with the numbers swapped.
    if(firstnumb < secondnumb):
        return productRecursion(secondnumb, firstnumb)
    # If the second number is not equal to 0, call the
    # method again recursively otherwise, return 0.
    elif(secondnumb != 0):
        return(firstnumb + productRecursion(firstnumb, secondnumb-1))
    else:
        return 0


# Give the two numbers as static input and store them as two variables.
firstnumb = 27
secondnumb = 19
# passing the given two numbers as arguments to the recursive function
# which returns the product of the given two numbers
# printing th product of th two numbers
print("The product of the given numbers", firstnumb, 'and', secondnumb, 'is ' +
      str(firstnumb)+' * '+str(secondnumb)+' = ', productRecursion(firstnumb, secondnumb))

Output:

The product of the given numbers 27 and 19 is 27 * 19 =  513

2)Using Recursion(User Input separated by spaces)

Approach:

  • Give the two numbers as user input using the map() and split() function and store them in two separate variables.
  • To find the product of the two numbers, pass the numbers as arguments to a recursive function.
  • Give the base condition that if the first number is less than the second, execute the method recursively with the numbers swapped.
  • If the second number is not equal to 0, call the method again recursively otherwise, return 0.
  • The function returns the product of the given two numbers.
  • Print the result using the print() function.
  • The exit of the program.

Below is the implementation:

# function who calculates the product of the given two numbers recursively
def productRecursion(firstnumb, secondnumb):
  # Give the base condition that if the first number is less than the second,
  # execute the method recursively with the numbers swapped.
    if(firstnumb < secondnumb):
        return productRecursion(secondnumb, firstnumb)
    # If the second number is not equal to 0, call the
    # method again recursively otherwise, return 0.
    elif(secondnumb != 0):
        return(firstnumb + productRecursion(firstnumb, secondnumb-1))
    else:
        return 0


# Give the two numbers as user input using the map() and split() function
# and store them in two separate variables.
firstnumb, secondnumb = map(int, input(
    'Enter some random numbers separated by spaces = ').split())
# passing the given two numbers as arguments to the recursive function
# which returns the product of the given two numbers
# printing th product of th two numbers
print("The product of the given numbers", firstnumb, 'and', secondnumb, 'is ' +
      str(firstnumb)+' * '+str(secondnumb)+' = ', productRecursion(firstnumb, secondnumb))

Output:

Enter some random numbers separated by spaces = 23 38
The product of the given numbers 23 and 38 is 23 * 38 = 874

Explanation:

  • The user must provide two integers.
  • To find the product of two numbers, the two numbers are sent as parameters to a recursive function.
  • The basic condition is that if the first number is less than the second, the function is called recursively with the inputs swapped.
  • If the second number is greater than zero, the function is called recursively; otherwise, 0 is returned.
  • The result of the two numbers is printed.

Related Programs:

Python Program to Print Inverted Pyramid Pattern with the Same Digit

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

Given the number of rows, the task is to print Inverted Pyramid Pattern with the same digit in C,C++, and Python

Examples:

Example1:

Input:

Given number of rows = 10

Output:

10 10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 
10 10 10 10 10 10 
10 10 10 10 10 
10 10 10 10 
10 10 10 
10 10 
10

Example2:

Input:

Given number of rows = 9

Output:

9 9 9 9 9 9 9 9 9 
9 9 9 9 9 9 9 9 
9 9 9 9 9 9 9 
9 9 9 9 9 9 
9 9 9 9 9 
9 9 9 9 
9 9 9 
9 9 
9

Program to Print Inverted Pyramid Pattern with the Same Digit in C, C++, and Python

Below are the ways to Program to Print Inverted Pyramid patterns with the Same Digit in C, C++, and Python.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Take a variable and store the given number of rows in it say givennumbrows.
  • Loop from the number of rows to 0 in decreasing order using For loop.
  • Loop from 0 to the parent loop iterator using another For loop(Nested For Loop).
  • Print the givennumbrows.
  • Print the Newline character after the end of the inner 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.
numbrrows = 7
# Take a variable and store the given number of rows in it say givennumbrows.
givennumbrows = numbrrows
# Loop from the number of rows to 0 in decreasing order using For loop.
for m in range(numbrrows, 0, -1):
    # Loop from 0 to the parent loop iterator using another For loop(Nested For Loop).
    for n in range(0, m):
        # Print the givennumbrows.
        print(givennumbrows, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

7 7 7 7 7 7 7 
7 7 7 7 7 7 
7 7 7 7 7 
7 7 7 7 
7 7 7 
7 7 
7

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 numbrrows = 10;
    // Take a variable and store the given number of rows in
    // it say givennumbrows.
    int givennumbrows = numbrrows;
    // Loop from the number of rows to 0 in decreasing
    // order using For loop.
    for (int m = numbrrows; m > 0; m--) {
        // Loop from 0 to the parent loop iterator using
        // another For loop(Nested For Loop).
        for (int n = 0; n < m; n++) {
            // Print the givennumbrows.
            cout << givennumbrows << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }
    return 0;
}

Output:

10 10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 
10 10 10 10 10 10 
10 10 10 10 10 
10 10 10 10 
10 10 10 
10 10 
10

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 numbrrows = 4;
    // Take a variable and store the given number of rows in
    // it say givennumbrows.
    int givennumbrows = numbrrows;
    // Loop from the number of rows to 0 in decreasing
    // order using For loop.
    for (int m = numbrrows; m > 0; m--) {
        // Loop from 0 to the parent loop iterator using
        // another For loop(Nested For Loop).
        for (int n = 0; n < m; n++) {
            // Print the givennumbrows.
            printf("%d ", givennumbrows);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

4 4 4 4 
4 4 4 
4 4 
4

Method #2: Using For Loop (User Input)

1) Python Implementation

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

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbrrows = int(input('Enter some random number of rows = '))
# Take a variable and store the given number of rows in it say givennumbrows.
givennumbrows = numbrrows
# Loop from the number of rows to 0 in decreasing order using For loop.
for m in range(numbrrows, 0, -1):
    # Loop from 0 to the parent loop iterator using another For loop(Nested For Loop).
    for n in range(0, m):
        # Print the givennumbrows.
        print(givennumbrows, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

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

2) C++ Implementation

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

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows as user input using
    // int(input()) and store it in a variable.
    int numbrrows;
    cin >> numbrrows;
    // Take a variable and store the given number of rows in
    // it say givennumbrows.
    int givennumbrows = numbrrows;
    // Loop from the number of rows to 0 in decreasing
    // order using For loop.
    for (int m = numbrrows; m > 0; m--) {
        // Loop from 0 to the parent loop iterator using
        // another For loop(Nested For Loop).
        for (int n = 0; n < m; n++) {
            // Print the givennumbrows.
            cout << givennumbrows << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

10
10 10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 10 
10 10 10 10 10 10 10 
10 10 10 10 10 10 
10 10 10 10 10 
10 10 10 10 
10 10 10 
10 10 
10

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 numbrrows;
    scanf("%d", &numbrrows);
    // Take a variable and store the given number of rows in
    // it say givennumbrows.
    int givennumbrows = numbrrows;
    // Loop from the number of rows to 0 in decreasing
    // order using For loop.
    for (int m = numbrrows; m > 0; m--) {
        // Loop from 0 to the parent loop iterator using
        // another For loop(Nested For Loop).
        for (int n = 0; n < m; n++) {
            // Print the givennumbrows.
            printf("%d ", givennumbrows);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

9
9 9 9 9 9 9 9 9 9 
9 9 9 9 9 9 9 9 
9 9 9 9 9 9 9 
9 9 9 9 9 9 
9 9 9 9 9 
9 9 9 9 
9 9 9 
9 9 
9

Related Programs:

Python Program to Print Pattern with a Combination of Numbers and Stars

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Given the number of rows, the task is to Print pattern with a combination of Numbers and Stars in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows = 8

Output:

1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 
1 * 2 * 3 * 4 * 5 * 6 * 7 
1 * 2 * 3 * 4 * 5 * 6 
1 * 2 * 3 * 4 * 5 
1 * 2 * 3 * 4 
1 * 2 * 3 
1 * 2 
1

Example2:

Input:

Given number of rows = 13

Output:

1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13 
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 
1 * 2 * 3 * 4 * 5 * 6 * 7 
1 * 2 * 3 * 4 * 5 * 6 
1 * 2 * 3 * 4 * 5 
1 * 2 * 3 * 4 
1 * 2 * 3 
1 * 2 
1

Program to Print Pattern with a Combination of Numbers and Stars in C, C++, and Python

Below are the ways to print Print patterns with a combination of Numbers and Stars in C, C++, and Python.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Inside the For, loop Take a variable(say Tempo) and initialize its value with 1.
  • Print the value of the Tempo variable with space.
  • Loop from the number of rows -m-1 to 0 in decreasing order using another for loop(Nested For loop) where m is the iterator value of the parent For loop.
  • Print the Star Character with space.
  • Increment the value of Tempo by 1.
  • Print the value of the Tempo variable with space.
  • Print the Newline character after the end of the inner 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.
numberOfRows = 8
# Loop from 0 to the number of rows using For loop.
for m in range(0, numberOfRows):
    # Inside the For, loop Take a variable(say Tempo) and initialize its value with 1.
    Tempo = 1
    # Print the value of the Tempo variable with space.
    print(Tempo, end=' ')
    # Loop from the number of rows -m-1 to 0 in decreasing order using another for loop(Nested For loop)
    # where m is the iterator value of the parent For loop.
    for n in range(numberOfRows-m-1, 0, -1):
      # Print the Star Character with space.
        print('*', end=' ')
        # Increment the value of Tempo by 1.
        Tempo = Tempo + 1
        # Print the value of the Tempo variable with space.
        print(Tempo, end=' ')

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

Output:

1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 
1 * 2 * 3 * 4 * 5 * 6 * 7 
1 * 2 * 3 * 4 * 5 * 6 
1 * 2 * 3 * 4 * 5 
1 * 2 * 3 * 4 
1 * 2 * 3 
1 * 2 
1

2) C++ Implementation

Below is the implementation:

#include <iostream>
#include <math.h>
using namespace std;
int main()
{

    // Give the number of rows as static input and store it
    // in a variable.
    int numberOfRows = 8;
    // Loop from 0 to the number of rows using For loop.

    for (int m = 0; m < numberOfRows; m++) {
        // Inside the For, loop Take a variable(say Tempo)
        // and initialize its value with 1.
        int Tempo = 1;
        // Print the value of the Tempo variable with space.
        cout << Tempo << " ";
        // Loop from the number of rows -m-1 to 0 in
        // decreasing order using another
        // for loop(Nested For loop)
        // where m is the iterator value of the parent For
        // loop.
        for (int n = numberOfRows - m - 1; n > 0; n--) {
            // Print the Star Character with space.
            cout << "* ";
            // Increment the value of Tempo by 1.
            Tempo = Tempo + 1;
            // Print the value of the Tempo variable with
            ///space.
            cout << Tempo << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 
1 * 2 * 3 * 4 * 5 * 6 * 7 
1 * 2 * 3 * 4 * 5 * 6 
1 * 2 * 3 * 4 * 5 
1 * 2 * 3 * 4 
1 * 2 * 3 
1 * 2 
1

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows as static input and store it
    // in a variable.
    int numberOfRows = 13;
    // Loop from 0 to the number of rows using For loop.

    for (int m = 0; m < numberOfRows; m++) {
        // Inside the For, loop Take a variable(say Tempo)
        // and initialize its value with 1.
        int Tempo = 1;
        // Print the value of the Tempo variable with space.
        printf("%d ", Tempo);
        // Loop from the number of rows -m-1 to 0 in
        // decreasing order using another
        // for loop(Nested For loop)
        // where m is the iterator value of the parent For
        // loop.
        for (int n = numberOfRows - m - 1; n > 0; n--) {
            // Print the Star Character with space.
            printf("* ");
            // Increment the value of Tempo by 1.
            Tempo = Tempo + 1;
            // Print the value of the Tempo variable with
            /// space.
            printf("%d ", Tempo);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 * 13 
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 * 12 
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 
1 * 2 * 3 * 4 * 5 * 6 * 7 
1 * 2 * 3 * 4 * 5 * 6 
1 * 2 * 3 * 4 * 5 
1 * 2 * 3 * 4 
1 * 2 * 3 
1 * 2 
1

Method #2: Using For Loop (User Input)

Approach:

  • Give the number of rows as user input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Inside the For, loop Take a variable(say Tempo) and initialize its value with 1.
  • Print the value of the Tempo variable with space.
  • Loop from the number of rows -m-1 to 0 in decreasing order using another for loop(Nested For loop) where m is the iterator value of the parent For loop.
  • Print the Star Character with space.
  • Increment the value of Tempo by 1.
  • Print the value of the Tempo variable with space.
  • Print the Newline character after the end of the inner 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 number of rows as user input using int(input()) and store it in a variable.
numberOfRows = int(input('Enter some random number of rows = '))
# Loop from 0 to the number of rows using For loop.
for m in range(0, numberOfRows):
    # Inside the For, loop Take a variable(say Tempo) and initialize its value with 1.
    Tempo = 1
    # Print the value of the Tempo variable with space.
    print(Tempo, end=' ')
    # Loop from the number of rows -m-1 to 0 in decreasing order using another for loop(Nested For loop)
    # where m is the iterator value of the parent For loop.
    for n in range(numberOfRows-m-1, 0, -1):
      # Print the Star Character with space.
        print('*', end=' ')
        # Increment the value of Tempo by 1.
        Tempo = Tempo + 1
        # Print the value of the Tempo variable with space.
        print(Tempo, end=' ')

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

Output:

Enter some random number of rows = 11
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 * 11 
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 
1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 
1 * 2 * 3 * 4 * 5 * 6 * 7 
1 * 2 * 3 * 4 * 5 * 6 
1 * 2 * 3 * 4 * 5 
1 * 2 * 3 * 4 
1 * 2 * 3 
1 * 2 
1

2) C++ Implementation

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

Below is the implementation:

#include <iostream>
using namespace std;
int main()
{

    // Give the number of rows as user input using
    // int(input()) and store it in a variable.
    int numberOfRows;
    cin >> numberOfRows;
    // Loop from 0 to the number of rows using For loop.

    for (int m = 0; m < numberOfRows; m++) {
        // Inside the For, loop Take a variable(say Tempo)
        // and initialize its value with 1.
        int Tempo = 1;
        // Print the value of the Tempo variable with space.
        cout << Tempo << " ";
        // Loop from the number of rows -m-1 to 0 in
        // decreasing order using another
        // for loop(Nested For loop)
        // where m is the iterator value of the parent For
        // loop.
        for (int n = numberOfRows - m - 1; n > 0; n--) {
            // Print the Star Character with space.
            cout << "* ";
            // Increment the value of Tempo by 1.
            Tempo = Tempo + 1;
            // Print the value of the Tempo variable with
            /// space.
            cout << Tempo << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

5
1 * 2 * 3 * 4 * 5 
1 * 2 * 3 * 4 
1 * 2 * 3 
1 * 2 
1

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 numberOfRows;
    scanf("%d", &numberOfRows);
   // Loop from 0 to the number of rows using For loop.

    for (int m = 0; m < numberOfRows; m++) {
        // Inside the For, loop Take a variable(say Tempo)
        // and initialize its value with 1.
        int Tempo = 1;
        // Print the value of the Tempo variable with space.
        printf("%d ", Tempo);
        // Loop from the number of rows -m-1 to 0 in
        // decreasing order using another
        // for loop(Nested For loop)
        // where m is the iterator value of the parent For
        // loop.
        for (int n = numberOfRows - m - 1; n > 0; n--) {
            // Print the Star Character with space.
            printf("* ");
            // Increment the value of Tempo by 1.
            Tempo = Tempo + 1;
            // Print the value of the Tempo variable with
            /// space.
            printf("%d ", Tempo);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

1 * 2 * 3 * 4 * 5 * 6 
1 * 2 * 3 * 4 * 5 
1 * 2 * 3 * 4 
1 * 2 * 3 
1 * 2 
1

Related Programs:

Python Program to Print the Pyramid of Horizontal Number Tables

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Given the number of rows, the task is to Print the Pyramid of Horizontal Number Tables in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows =9

Output:

1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81

Example2:

Input:

Given number of rows =13

Output:

1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81 
10 20 30 40 50 60 70 80 90 100 
11 22 33 44 55 66 77 88 99 110 121 
12 24 36 48 60 72 84 96 108 120 132 144 
13 26 39 52 65 78 91 104 117 130 143 156 169

Program to Print the Pyramid of Horizontal Number Tables in C, C++, and Python

Below are the ways to Print the Pyramid of Horizontal Number Tables in C, C++, and Python.

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to the number of 1 to parent loop iterator value using another For loop(Nested For Loop).
  • Print the value of the product of parent loop iterator value and inner loop iterator value 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 as static input and store it in a variable.
numberows = 9
# Loop from 1 to the number of rows using For loop.
for m in range(1, numberows+1):
    # Loop from 1 to the number of 1 to parent loop iterator value
    # using another For loop(Nested For Loop).
    for n in range(1, m+1):
        # Print the value of the product of parent loop iterator value
        # and inner loop iterator value with space.
        print(m*n, end=" ")
     # Print the newline character after the end of the inner For loop.
    print()

Output:

1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81

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 the number of rows using For loop.
    for (int m = 1; m <= numberows; m++) {
        // Loop from 1 to the number of 1 to parent loop
        // iterator value
        // using another For loop(Nested For Loop).
        for (int n = 1; n <= m; n++) {
            // Print the value of the product of parent loop
            // iterator value and inner loop iterator value
            // with space.
            cout << m * n << " ";
        }
        // Print the newline character after the end of
        // the inner For loop.
        cout << endl;
    }

    return 0;
}

Output:

1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81

3) C Implementation

Below is the implementation:

#include <stdio.h>
#include <string.h>
int main()
{

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

    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numberows; m++) {
        // Loop from 1 to the number of 1 to parent loop
        // iterator value
        // using another For loop(Nested For Loop).
        for (int n = 1; n <= m; n++) {
            // Print the value of the product of parent loop
            // iterator value and inner loop iterator value
            // with space.
            printf("%d ", m * n);
        }
        // Print the newline character after the end of
        // the inner For loop.
        printf("\n");
    }
    return 0;
}

Output:

1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to the number of 1 to parent loop iterator value using another For loop(Nested For Loop).
  • Print the value of the product of the parent loop iterator value and inner loop iterator value 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.

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 = '))
# Loop from 1 to the number of rows using For loop.
for m in range(1, numberows+1):
    # Loop from 1 to the number of 1 to parent loop iterator value
    # using another For loop(Nested For Loop).
    for n in range(1, m+1):
        # Print the value of the product of parent loop iterator value
        # and inner loop iterator value with space.
        print(m*n, end=" ")
     # Print the newline character after the end of the inner For loop.
    print()

Output:

Enter some random number of rows = 13
1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81 
10 20 30 40 50 60 70 80 90 100 
11 22 33 44 55 66 77 88 99 110 121 
12 24 36 48 60 72 84 96 108 120 132 144 
13 26 39 52 65 78 91 104 117 130 143 156 169

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;
    cin >> numberows;

    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numberows; m++) {
        // Loop from 1 to the number of 1 to parent loop
        // iterator value
        // using another For loop(Nested For Loop).
        for (int n = 1; n <= m; n++) {
            // Print the value of the product of parent loop
            // iterator value and inner loop iterator value
            // with space.
            cout << m * n << " ";
        }
        // Print the newline character after the end of
        // the inner For loop.
        cout << endl;
    }

    return 0;
}

Output:

13
1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81 
10 20 30 40 50 60 70 80 90 100 
11 22 33 44 55 66 77 88 99 110 121 
12 24 36 48 60 72 84 96 108 120 132 144 
13 26 39 52 65 78 91 104 117 130 143 156 169

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>
#include <string.h>
int main()
{

    // Give the number of rows as user input using scanf and
    // store it in a variable.
    int numberows;
    scanf("%d", &numberows);

    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numberows; m++) {
        // Loop from 1 to the number of 1 to parent loop
        // iterator value
        // using another For loop(Nested For Loop).
        for (int n = 1; n <= m; n++) {
            // Print the value of the product of parent loop
            // iterator value and inner loop iterator value
            // with space.
            printf("%d ", m * n);
        }
        // Print the newline character after the end of
        // the inner For loop.
        printf("\n");
    }
    return 0;
}

Output:

13
1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 
6 12 18 24 30 36 
7 14 21 28 35 42 49 
8 16 24 32 40 48 56 64 
9 18 27 36 45 54 63 72 81 
10 20 30 40 50 60 70 80 90 100 
11 22 33 44 55 66 77 88 99 110 121 
12 24 36 48 60 72 84 96 108 120 132 144 
13 26 39 52 65 78 91 104 117 130 143 156 169

Related Programs:

Python Program to Print Pattern to Display Letters of the Word

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Given a word the task is to print the letters of the word in C, C++, and Python.

Examples:

Example1:

Input:

Given string ="BTechGeeks"

Output:

B
BT
BTe
BTec
BTech
BTechG
BTechGe
BTechGee
BTechGeek
BTechGeeks

Example2:

Input:

Given string ="Aplustopper"

Output:

A
Ap
Apl
Aplu
Aplus
Aplust
Aplusto
Aplustop
Aplustopp
Aplustoppe
Aplustopper

Pattern to display letters of the word in C, C++, and Python.

Below are the ways to display letters of the word in C, C++, and Python.

Method #1: Using For loop (Static Input)

Approach:

  • Give the word as static input and store it in a variable.
  • Take an empty string and store it in a variable say sampstrng.
  • Traverse the given word(string) using For loop.
  • Concatenate the iterator character to the sampstrng using string Concatenation.
  • Print the sampstrng.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the word as static input and store it in a variable.
givenword = 'BTechGeeks'
# Take an empty string and store it in a variable say sampstrng.
sampstrng = ""
# Traverse the given word(string) using For loop.
for wordchar in givenword:
    # Concatenate the iterator character to the sampstrng using string Concatenation.
    sampstrng = sampstrng+wordchar
    # Print the sampstrng.
    print(sampstrng)

Output:

B
BT
BTe
BTec
BTech
BTechG
BTechGe
BTechGee
BTechGeek
BTechGeeks

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the word as static input and store it in a
    // variable.
    string givenword = "BTechGeeks";
    // Take an empty string and store it in a variable say
    // sampstrng.
    string sampstrng = "";
    // Traverse the given word(string) using For loop.
    for (int wordchar = 0; wordchar < givenword.length();
         wordchar++) {
        // Concatenate the iterator character to the
        // sampstrng using string Concatenation.

        sampstrng = sampstrng + givenword[wordchar];
        // Print the sampstrng.
        cout << sampstrng << endl;
    }
    return 0;
}

Output:

B
BT
BTe
BTec
BTech
BTechG
BTechGe
BTechGee
BTechGeek
BTechGeeks

3) C Implementation

Below is the implementation:

#include <stdio.h>
#include <string.h>
int main()
{

    // Give the word as static input and store it in a
    // variable.
    char givenword[100] = "BTechGeeks";
    // Take an empty string and store it in a variable say
    // sampstrng.
    char sampstrng[100] = "";
    // Traverse the given word(string) using For loop.
    for (int wordchar = 0; wordchar < strlen(givenword);
         wordchar++) {
        // Concatenate the iterator character to the
        // sampstrng using string Concatenation.

        sampstrng[wordchar] = givenword[wordchar];
        // Print the sampstrng.
        printf("%s", sampstrng);
        printf("\n");
    }
    return 0;
}

Output:

B
BT
BTe
BTec
BTech
BTechG
BTechGe
BTechGee
BTechGeek
BTechGeeks

Method #2: Using For loop (User Input)

Approach:

  • Give the word as user input and store it in a variable.
  • Take an empty string and store it in a variable say sampstrng.
  • Traverse the given word(string) using For loop.
  • Concatenate the iterator character to the sampstrng using string Concatenation.
  • Print the sampstrng.
  • The Exit of the Program.

1) Python Implementation

Give the word as user input using input() and store it in a variable.

Below is the implementation:

# Give the word as user input and store it in a variable.
givenword = input('Enter some random word = ')
# Take an empty string and store it in a variable say sampstrng.
sampstrng = ""
# Traverse the given word(string) using For loop.
for wordchar in givenword:
    # Concatenate the iterator character to the sampstrng using string Concatenation.
    sampstrng = sampstrng+wordchar
    # Print the sampstrng.
    print(sampstrng)

Output:

Enter some random word = Aplustopper
A
Ap
Apl
Aplu
Aplus
Aplust
Aplusto
Aplustop
Aplustopp
Aplustoppe
Aplustopper

2) C++ Implementation

Give the word as user input using cin and store it in a variable.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the word as user input using cin and store it in
    // a variable.
    string givenword;
    cin >> givenword;
    // Take an empty string and store it in a variable say
    // sampstrng.
    string sampstrng = "";
    // Traverse the given word(string) using For loop.
    for (int wordchar = 0; wordchar < givenword.length();
         wordchar++) {
        // Concatenate the iterator character to the
        // sampstrng using string Concatenation.

        sampstrng = sampstrng + givenword[wordchar];
        // Print the sampstrng.
        cout << sampstrng << endl;
    }
    return 0;
}

Output:

Aplustopper
A
Ap
Apl
Aplu
Aplus
Aplust
Aplusto
Aplustop
Aplustopp
Aplustoppe
Aplustopper

3) C Implementation

Give the word as user input using scanf and store it in a variable.

Below is the implementation

#include <stdio.h>
#include <string.h>
int main()
{

    // Give the word as user input using scanfand store it
    // in a variable.
    char givenword[100];
    scanf("%s", givenword);
    // Take an empty string and store it in a variable say
    // sampstrng.
    char sampstrng[100] = "";
    // Traverse the given word(string) using For loop.
    for (int wordchar = 0; wordchar < strlen(givenword);
         wordchar++) {
        // Concatenate the iterator character to the
        // sampstrng using string Concatenation.

        sampstrng[wordchar] = givenword[wordchar];
        // Print the sampstrng.
        printf("%s", sampstrng);
        printf("\n");
    }
    return 0;
}

Output:

Aplustopper
A
Ap
Apl
Aplu
Aplus
Aplust
Aplusto
Aplustop
Aplustopp
Aplustoppe
Aplustopper

Related Programs:

Python Program to Print a Unique Pyramid Pattern of Digits

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Given the number of rows, the task is to Print a Unique Pyramid Pattern of Digits in C, C++, and Python

Examples:

Example1:

Input:

Given number of rows = 8

Output:

1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1 
1 2 3 4 5 6 7 6 5 4 3 2 1

Example2:

Input:

Given number of rows = 7

Output:

1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1

Program to Print a Unique Pyramid Pattern of Digits in C, C++, and Python

Below are the ways to Print a Unique Pyramid Pattern of Digits in C, C++, and Python

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to m-1 using another For loop(Nested For loop) where m is the iterator value of the parent For loop.
  • Print the inner loop iterator value with space.
  • Loop from m-1 to 0 in decreasing order using another For loop(Nested For loop) where m is the iterator value of the parent For loop.
  • Print the inner loop iterator value with space.
  • Print the Newline character after the end of the inner 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.
numberOfRows = 8
# Loop from 1 to the number of rows using For loop.
for m in range(1, numberOfRows + 1):
    # Loop from 1 to m-1 using another For loop(Nested For loop)
    # where m is the iterator value of the parent For loop.
    for n in range(1, m - 1):
      # Print the inner loop iterator value with space.
        print(n, end=" ")
    # Loop from m-1 to 0 in decreasing order using another For loop(Nested For loop)
    # where m is the iterator value of the parent For loop.
    for n in range(m - 1, 0, -1):
      # Print the inner loop iterator value with space.
        print(n, end=" ")
    # Print the Newline character after the end of the inner loop.
    print()

Output:

1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1 
1 2 3 4 5 6 7 6 5 4 3 2 1

2) C++ Implementation

Below is the implementation:

#include <iostream>
#include <math.h>
using namespace std;
int main()
{

    // Give the number of rows as static input and store it
    // in a variable.
    int numberOfRows = 8;
    // Loop from 1 to the number of rows using For loop.

    for (int m = 1; m <= numberOfRows; m++) {
        // Loop from 1 to m-1 using another For loop(Nested
        // For loop)
        // where m is the iterator value of the parent For
        // loop.
        for (int n = 1; n < m - 1; n++) {
            // Print the inner loop iterator value with
            // space.
            cout << n << " ";
        }
        // Loop from m-1 to 0 in decreasing order using
        // another For loop(Nested For loop) where m is the
        // iterator value of the parent For loop.
        for (int n = m - 1; n > 0; n--) {
            // Print the inner loop iterator value with
            // space.
            cout << n << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1 
1 2 3 4 5 6 7 6 5 4 3 2 1

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows as static input and store it
    // in a variable.
    int numberOfRows = 7;
    // Loop from 1 to the number of rows using For loop.

    for (int m = 1; m <= numberOfRows; m++) {
        // Loop from 1 to m-1 using another For loop(Nested
        // For loop) where m is the iterator value of the
        // parent For loop.
        for (int n = 1; n < m - 1; n++) {
            // Print the inner loop iterator value with
            // space.
            printf("%d ", n);
        }
        // Loop from m-1 to 0 in decreasing order using
        // another For loop(Nested For loop) where m is the
        // iterator value of the parent For loop.
        for (int n = m - 1; n > 0; n--) {
            // Print the inner loop iterator value with
            // space.
            printf("%d ", n);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1

Method #2: Using For Loop (User Input)

Approach:

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

Output:

Enter some random number of rows = 17
1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1 
1 2 3 4 5 6 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 11 12 11 10 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 11 12 13 12 11 10 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 13 12 11 10 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

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>
#include <math.h>
using namespace std;
int main()
{

    // Give the number of rows as user input using
    // int(input()) and store it in a variable.
    int numberOfRows;
    cin >> numberOfRows;
    // Loop from 1 to the number of rows using For loop.

    for (int m = 1; m <= numberOfRows; m++) {
        // Loop from 1 to m-1 using another For loop(Nested
        // For loop)
        // where m is the iterator value of the parent For
        // loop.
        for (int n = 1; n < m - 1; n++) {
            // Print the inner loop iterator value with
            // space.
            cout << n << " ";
        }
        // Loop from m-1 to 0 in decreasing order using
        // another For loop(Nested For loop) where m is the
        // iterator value of the parent For loop.
        for (int n = m - 1; n > 0; n--) {
            // Print the inner loop iterator value with
            // space.
            cout << n << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

5
1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 

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 numberOfRows;
    scanf("%d", &numberOfRows);
    // Loop from 1 to the number of rows using For loop.

    for (int m = 1; m <= numberOfRows; m++) {
        // Loop from 1 to m-1 using another For loop(Nested
        // For loop) where m is the iterator value of the
        // parent For loop.
        for (int n = 1; n < m - 1; n++) {
            // Print the inner loop iterator value with
            // space.
            printf("%d ", n);
        }
        // Loop from m-1 to 0 in decreasing order using
        // another For loop(Nested For loop) where m is the
        // iterator value of the parent For loop.
        for (int n = m - 1; n > 0; n--) {
            // Print the inner loop iterator value with
            // space.
            printf("%d ", n);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

6
1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1

Related Programs:

Python Program to Form a Dictionary from an Object of a Class

Program to Form a Dictionary from an Object of a Class

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Object-Oriented Programming in Python:

There are certain advantages to using object-oriented programming over alternative design patterns. Development is faster and less expensive, with improved program maintainability. As a result, the higher-quality program that is also extendable with additional methods and properties is produced. The learning curve, on the other hand, is steeper. For beginners, the notion may be too difficult. The OOPs program is slower to compute and consumes more memory since more lines of code must be written.

Object-oriented programming is built on the imperative programming paradigm, which uses statements to alter the state of a program. It is concerned with specifying how the program should work.

C, C++, Java, Go, Ruby, and Python are examples of imperative programming languages. This is in contrast to declarative programming, which focuses on what the computer program should do without stating how it should do it. Database query languages such as SQL and XQuery are examples, where one tells the machine not only what data to seek from where, but also how to do it.

Object-oriented programming (OOP) makes use of the concepts of objects and classes. A class can be viewed as a “blueprint” for things. These can each have their own set of traits (characteristics) and procedures (actions they perform).

In this post, we will look at how to get a dictionary from an object’s field, or how to retrieve the class members as a dictionary. There are two techniques to resolving the preceding problem:

By applying the __dict__ attribute to a class object and obtaining the dictionary. In Python, all objects have the attribute __dict__, which is a dictionary object holding all of the attributes defined for that object. A dictionary is created by mapping attributes to their values.
By invoking the built-in vars method, which returns the __dict__ attribute of a module, class, class instance, or object.

Program to Form a Dictionary from an Object of a Class in Python

There are two ways to create a dictionary from an object of a class in Python they are:

Method #1:Using __dict__ attribute

Approach:

  • Create a class called SamplClass.
  • In the class’s __init__ method, initialize the keys with their values and create a dictionary with the __dict__ method.
  • Print the dictionary created from the class’s object.
  • The Exit of the Program.

Below is the implementation:

# creating new class
class SamplClass:

    # default constructor which initialzes the values
    def __init__(self):

        # keys are initialized with
        # their respective values
        self.examp1 = 'hello'
        self.examp2 = 'this'
        self.examp3 = 'is'
        self.examp4 = 'btechGeeks'
    # printDict method which prints some sample text

    def printDict(self):
        print("Dictionary constructed from the object fields of the class SamplClass:")


# Creating an object to represent the class.
sampleObjec = SamplClass()

# calling printit method
sampleObjec.printDict()
# calling object __dict__ on SamplClass object and printing it
print(sampleObjec.__dict__)

Output:

Dictionary constructed from the object fields of the class SamplClass:
{'examp1': 'hello', 'examp2': 'this', 'examp3': 'is', 'examp4': 'btechGeeks'}

Explanation:

  • A class named SamplClass has been declared.
  • The keys are initialized with their values in the class’s __init__ method.
  • The dictionary is created by using the class object and the __dict__ method.
  • The dictionary constructed from the class’s object is printed.

Method #2:Using vars method

To create a dictionary from an arbitrary object, use the built-in vars method.

Approach:

  • Create a class called SamplClass.
  • In the class’s __init__ method, initialize the keys with their values and create a dictionary with the var() method.
  • Print the dictionary created from the class’s object.
  • The Exit of the Program.

Below is the implementation:

# creating new class
class SamplClass:

    # default constructor which initialzes the values
    def __init__(self):

        # keys are initialized with
        # their respective values
        self.examp1 = 'hello'
        self.examp2 = 'this'
        self.examp3 = 'is'
        self.examp4 = 'btechGeeks'
        self.examp5 = 'online'
        self.examp6 = 'coding'
        self.examp7 = 'platform'
        self.examp8 = 'for'
        self.examp9 = 'students'
    # printDict method which prints some sample text

    def printDict(self):
        print("Dictionary constructed from the object fields of the class SamplClass:")


# Creating an object to represent the class.
sampleObjec = SamplClass()

# calling printit method
sampleObjec.printDict()
# calling vars method on the object on SamplClass object and printing it
print(vars(sampleObjec))

Output:

Dictionary constructed from the object fields of the class SamplClass:
{'examp1': 'hello', 'examp2': 'this', 'examp3': 'is', 'examp4': 'btechGeeks', 'examp5': 'online', 'examp6': 'coding', 
'examp7': 'platform', 'examp8': 'for', 'examp9': 'students'}

Related Programs:

Number Pattern Programs in Python

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

1) Number Pattern – 1

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 1 to the number of rows using For Loop.
  • Loop from 1 to iterator value of the parent For loop using another For loop(Inner For Loop).
  • Print the iterator value of the inner For loop.
  • Print the Newline character after the end of the inner loop.
  • The Exit of the Program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 8
# Loop from 1 to the number of rows using For Loop.
for m in range(1, numbOfRows+1):
    # Loop from 1 to iterator value of the parent For loop
    # using another For loop(Inner For Loop).
    for n in range(1, m+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from 1 to the number of rows using For Loop.
  • Loop from 1 to iterator value of the parent For loop using another For loop(Inner For Loop).
  • Print the iterator value of the inner For loop.
  • Print the Newline character after the end of the inner loop.
  • The Exit of the Program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For Loop.
for m in range(1, numbOfRows+1):
    # Loop from 1 to iterator value of the parent For loop
    # using another For loop(Inner For Loop).
    for n in range(1, m+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

Enter some random number of rows = 8
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8

2) Number Pattern – 2

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 1 to the number of rows using For Loop.
  • Loop from 1 to iterator value of the parent For loop using another For loop(Inner For Loop).
  • Print the iterator value of the inner For loop.
  • Print the Newline character after the end of the inner loop.
  • Loop from the number of rows-1 to 1 in decreasing order using For loop.
  • Loop from 1 to iterator value of the parent For loop using another For loop(Inner For Loop).
  • Print the iterator value of the inner For loop.
  • Print the Newline character after the end of the inner loop.
  • The Exit of the Program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 6
# Loop from 1 to the number of rows using For Loop.
for m in range(1, numbOfRows+1):
    # Loop from 1 to iterator value of the parent For loop
    # using another For loop(Inner For Loop).
    for n in range(1, m+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()
# Loop from the number of rows-1 to 1 in decreasing order using For loop.
for m in range(numbOfRows-1, 0, -1):
    # Loop from 1 to iterator value of the parent For loop
    # using another For loop(Inner For Loop).
    for n in range(1, m+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from 1 to the number of rows using For Loop.
  • Loop from 1 to iterator value of the parent For loop using another For loop(Inner For Loop).
  • Print the iterator value of the inner For loop.
  • Print the Newline character after the end of the inner loop.
  • Loop from the number of rows-1 to 1 in decreasing order using For loop.
  • Loop from 1 to iterator value of the parent For loop using another For loop(Inner For Loop).
  • Print the iterator value of the inner For loop.
  • Print the Newline character after the end of the inner loop.
  • The Exit of the Program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For Loop.
for m in range(1, numbOfRows+1):
    # Loop from 1 to iterator value of the parent For loop
    # using another For loop(Inner For Loop).
    for n in range(1, m+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()
# Loop from the number of rows-1 to 1 in decreasing order using For loop.
for m in range(numbOfRows-1, 0, -1):
    # Loop from 1 to iterator value of the parent For loop
    # using another For loop(Inner For Loop).
    for n in range(1, m+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

Enter some random number of rows = 8
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 
1 2 3 4 5 6 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1

3) Number Pattern – 3

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows 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.
  • Print the iterator value of the parent loop with space in the inner For loop. (This prints the same number parent loop number of times)
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 8
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbOfRows+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # Print the iterator value of the nested loop with space in the inner For loop.
        # (This prints the same number parent loop number of times)
        print(m, end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output:

1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 6 
7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(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.
  • Print the iterator value of the parent loop with space in the inner For loop. (This prints the same number parent loop number of times)
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbOfRows+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # Print the iterator value of the nested loop with space in the inner For loop.
        # (This prints the same number parent loop number of times)
        print(m, end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output:

Enter some random number of rows = 6
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 6

4) Number Pattern – 4

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from the number of rows to iterator value>=m in decreasing order using another For Loop(Inner For loop) where m is the iterator value of the parent For loop.
  • Print the iterator value of the inner For loop.
  • Print the Newline character after the end of the inner loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 10
# Loop from 1 to the number of rows using For Loop.
for m in range(1, numbOfRows+1):
    # Loop from the number of rows to iterator value>=m in decreasing order
    # using another For Loop(Inner For loop)
    # where m is the iterator value of the parent For loop.
    for n in range(numbOfRows, m-1, -1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

10 9 8 7 6 5 4 3 2 1 
10 9 8 7 6 5 4 3 2 
10 9 8 7 6 5 4 3 
10 9 8 7 6 5 4 
10 9 8 7 6 5 
10 9 8 7 6 
10 9 8 7 
10 9 8 
10 9 
10

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from the number of rows to m in decreasing order using another For Loop(Inner For loop) where m is the iterator value of the parent For loop.
  • Print the iterator value of the inner For loop.
  • Print the Newline character after the end of the inner loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For Loop.
for m in range(1, numbOfRows+1):
    # Loop from the number of rows to iterator value>=m in decreasing order
    # using another For Loop(Inner For loop)
    # where m is the iterator value of the parent For loop.
    for n in range(numbOfRows, m-1, -1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

Enter some random number of rows = 9
9 8 7 6 5 4 3 2 1 
9 8 7 6 5 4 3 2 
9 8 7 6 5 4 3 
9 8 7 6 5 4 
9 8 7 6 5 
9 8 7 6 
9 8 7 
9 8 
9

5) Number Pattern – 5

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from the number of rows to 1(included) in decreasing order.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 9
# Loop from the number of rows to 1(included) in decreasing order.
for m in range(numbOfRows, 0, -1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output:

1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 
1 2 3 4 5 6 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from the number of rows to 1(included) in decreasing order.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from the number of rows to 1(included) in decreasing order.
for m in range(numbOfRows, 0, -1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output:

Enter some random number of rows = 5
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1

6) Number Pattern – 6

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from the number of rows to 1(included) in decreasing order using For loop.
  • Loop from iterator value of the parent For loop to 1(included) in decreasing order using another Nested For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 11
# Loop from the number of rows to 1(included) in decreasing order using For loop.
for m in range(numbOfRows, 0, -1):
    # Loop from iterator value of the parent For loop to 1(included)
    # in decreasing order using another Nested For loop.
    for n in range(m, 0, -1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output:

11 10 9 8 7 6 5 4 3 2 1 
10 9 8 7 6 5 4 3 2 1 
9 8 7 6 5 4 3 2 1 
8 7 6 5 4 3 2 1 
7 6 5 4 3 2 1 
6 5 4 3 2 1 
5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from the number of rows to 1(included) in decreasing order using For loop.
  • Loop from iterator value of the parent For loop to 1(included) in decreasing order using another Nested For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from the number of rows to 1(included) in decreasing order using For loop.
for m in range(numbOfRows, 0, -1):
    # Loop from iterator value of the parent For loop to 1(included)
    # in decreasing order using another Nested For loop.
    for n in range(m, 0, -1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output:

Enter some random number of rows = 8
8 7 6 5 4 3 2 1 
7 6 5 4 3 2 1 
6 5 4 3 2 1 
5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1

7) Number Pattern – 7

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from the number of rows to 1(included) in decreasing order using For loop.
  • Loop from the number of rows to m in decreasing order using another For Loop(Inner For loop) where m is the iterator value of the parent For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows =6
# Loop from the number of rows to 1(included) in decreasing order.
for m in range(numbOfRows, 0, -1):
    # Loop from the number of rows to m in decreasing order using
    # another For Loop(Inner For loop)
    # where m is the iterator value of the parent For loop.
    for n in range(numbOfRows, m-1, -1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output:

6 
6 5 
6 5 4 
6 5 4 3 
6 5 4 3 2 
6 5 4 3 2 1

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from the number of rows to 1(included) in decreasing order using For loop.
  • Loop from iterator value of the parent For loop to 1(included) in decreasing order using another Nested For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

Output:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from the number of rows to 1(included) in decreasing order using For loop.
for m in range(numbOfRows, 0, -1):
    # Loop from the number of rows to m in decreasing order using
    # another For Loop(Inner For loop)
    # where m is the iterator value of the parent For loop.
    for n in range(numbOfRows, m-1, -1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output:

Enter some random number of rows = 8
8 
8 7 
8 7 6 
8 7 6 5 
8 7 6 5 4 
8 7 6 5 4 3 
8 7 6 5 4 3 2 
8 7 6 5 4 3 2 1

8) Number Pattern – 8

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows 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.
  • Print the iterator value of the inner For loop.
  • Loop from iterator value of the parent For loop -1 to 1(included) in decreasing order using another For loop(Inner For Loop).
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 9
# Loop from 1 to the number of rows using For Loop.
for m in range(1, numbOfRows+1):
    # Loop from 1 to iterator value of the parent For loop
    # using another For loop(Inner For Loop).
    for n in range(1, m+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    '''Loop from iterator value of the parent For loop -1 to 1(included) 
    in decreasing order using another For loop(Inner For Loop). '''
    for n in range(m-1, 0, -1):
       # Print the iterator value of the inner For loop.
        print(n, end=' ')

    # Print the Newline character after the end of the inner loop.
    print(

Output:

1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1 
1 2 3 4 5 6 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(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.
  • Print the iterator value of the inner For loop.
  • Loop from iterator value of the parent For loop -1 to 1(included) in decreasing order using another For loop(Inner For Loop).
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For Loop.
for m in range(1, numbOfRows+1):
    # Loop from 1 to iterator value of the parent For loop
    # using another For loop(Inner For Loop).
    for n in range(1, m+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    '''Loop from iterator value of the parent For loop -1 to 1(included) 
    in decreasing order using another For loop(Inner For Loop). '''
    for n in range(m-1, 0, -1):
       # Print the iterator value of the inner For loop.
        print(n, end=' ')

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

Output:

Enter some random number of rows = 10
1 
1 2 1 
1 2 3 2 1 
1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 
1 2 3 4 5 6 5 4 3 2 1 
1 2 3 4 5 6 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 
1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1

9) Number Pattern – 9

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from the number of rows to 1(included) in decreasing order.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • Loop from 2 to the number of rows using For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 8
# Loop from the number of rows to 1(included) in decreasing order.
for m in range(numbOfRows, 0, -1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the newline character after ending of inner For loop.
    print()
# Loop from 2 to the number of rows using For Loop.
for m in range(2, numbOfRows+1):
    # Loop from 1 to iterator value of the parent For loop
    # using another For loop(Inner For Loop).
    for n in range(1, m+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 
1 2 3 4 5 6 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from the number of rows to 1(included) in decreasing order.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • Loop from 2 to the number of rows using For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from the number of rows to 1(included) in decreasing order.
for m in range(numbOfRows, 0, -1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the newline character after ending of inner For loop.
    print()
# Loop from 2 to the number of rows using For Loop.
for m in range(2, numbOfRows+1):
    # Loop from 1 to iterator value of the parent For loop
    # using another For loop(Inner For Loop).
    for n in range(1, m+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

Enter some random number of rows = 11
1 2 3 4 5 6 7 8 9 10 11 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 
1 2 3 4 5 6 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 11

10) Number Pattern – 10

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from the 1 to the number of rows using For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print Two spaces.
  • Loop from the first loop iterator value to the number of rows(included) using Another For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • Loop from the number of rows -1 to 1(included) in decreasing order using For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print Two spaces.
  • Loop from the first loop iterator value to the number of rows(included) using Another For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 8
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbOfRows+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m):
        # Print Two spaces.
        print(' ', end=' ')
    # Loop from the first loop iterator value to the number of rows(included)
    # using Another For loop.
    for n in range(m, numbOfRows+1):
        print(n, end=' ')

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

# Loop from the number of rows -1 to 1(included) in decreasing order using For loop.
for m in range(numbOfRows-1, 0, -1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m):
        # Print Two spaces.
        print(' ', end=' ')
    # Loop from the first loop iterator value to the number of rows(included)
    # using Another For loop.
    for n in range(m, numbOfRows+1):
        print(n, end=' ')

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

Output:

1 2 3 4 5 6 7 8 
   2 3 4 5 6 7 8 
      3 4 5 6 7 8 
         4 5 6 7 8 
            5 6 7 8 
               6 7 8 
                  7 8 
                     8 
                  7 8 
               6 7 8 
            5 6 7 8 
         4 5 6 7 8  
      3 4 5 6 7 8  
   2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from the 1 to the number of rows using For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print Two spaces.
  • Loop from the first loop iterator value to the number of rows(included) using Another For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • Loop from the number of rows -1 to 1(included) in decreasing order using For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print Two spaces.
  • Loop from the first loop iterator value to the number of rows(included) using Another For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbOfRows+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m):
        # Print Two spaces.
        print(' ', end=' ')
    # Loop from the first loop iterator value to the number of rows(included)
    # using Another For loop.
    for n in range(m, numbOfRows+1):
        print(n, end=' ')

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

# Loop from the number of rows -1 to 1(included) in decreasing order using For loop.
for m in range(numbOfRows-1, 0, -1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m):
        # Print Two spaces.
        print(' ', end=' ')
    # Loop from the first loop iterator value to the number of rows(included)
    # using Another For loop.
    for n in range(m, numbOfRows+1):
        print(n, end=' ')

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

Output:

Enter some random number of rows = 8
1 2 3 4 5 6 7 8 
   2 3 4 5 6 7 8 
      3 4 5 6 7 8 
         4 5 6 7 8 
            5 6 7 8 
               6 7 8 
                  7 8 
                     8 
                  7 8 
               6 7 8 
            5 6 7 8 
         4 5 6 7 8  
      3 4 5 6 7 8  
   2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8

11) Number Pattern – 11

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from iterator value of the parent For loop to 1(included) in decreasing order using another Nested For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 10
# Loop from 1 to the number of rows using For Loop.
for m in range(1, numbOfRows+1):
    # Loop from iterator value of the parent For loop to 1(included)
    # in decreasing order
    # using another Nested For loop.
    for n in range(m, 0, -1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1 
6 5 4 3 2 1 
7 6 5 4 3 2 1 
8 7 6 5 4 3 2 1 
9 8 7 6 5 4 3 2 1 
10 9 8 7 6 5 4 3 2 1

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from iterator value of the parent For loop to 1(included) in decreasing order using another Nested For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For Loop.
for m in range(1, numbOfRows+1):
    # Loop from iterator value of the parent For loop to 1(included)
    # in decreasing order
    # using another Nested For loop.
    for n in range(m, 0, -1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the Newline character after the end of the inner loop.
    print()

Output:

Enter some random number of rows = 7
1 
2 1 
3 2 1 
4 3 2 1 
5 4 3 2 1 
6 5 4 3 2 1 
7 6 5 4 3 2 1

12) Number Pattern – 12

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows 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.
  • Check if the iterator value of the inner For loop is divisible by 2 or not using the If statement.
  • If it is true then print 0.
  • Else print 1.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 8
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbOfRows+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # Check if the iterator value of the inner For loop is divisible by 2
        # or not using the If statement.
        if(n % 2 == 0):
            # If it is true then print 0.
            print('0', end='')
           # Else print 1.
        else:
            print('1', end='')

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

Output:

1
10
101
1010
10101
101010
1010101
10101010

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(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.
  • Check if the iterator value of the inner For loop is divisible by 2 or not using the If statement.
  • If it is true then print 0.
  • Else print 1.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbOfRows+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # Check if the iterator value of the inner For loop is divisible by 2
        # or not using the If statement.
        if(n % 2 == 0):
            # If it is true then print 0.
            print('0', end='')
           # Else print 1.
        else:
            print('1', end='')

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

Output:

Enter some random number of rows = 11
1
10
101
1010
10101
101010
1010101
10101010
101010101
1010101010
10101010101

13) Number Pattern – 13

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to the number of rows using another Nested For loop.
  • Check if the iterator value of the inner For loop is equal to the iterator value of the parent For loop or not using the If statement.
  • If it is true then print the iterator value of the inner For Loop.
  • Else print 0.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 8
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbOfRows+1):
    # Loop from 1 to the number of rows using another Nested For loop.
    for n in range(1, numbOfRows+1):
        # Check if the iterator value of the inner For loop is equal to the iterator value of the parent
        # For loop or not using the If statement.
        if(n == m):
            # If it is true then print n.
            print(n, end=' ')
           # Else print 0.
        else:
            print('0', end=' ')

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

Output:

1 0 0 0 0 0 0 0 
0 2 0 0 0 0 0 0 
0 0 3 0 0 0 0 0 
0 0 0 4 0 0 0 0 
0 0 0 0 5 0 0 0 
0 0 0 0 0 6 0 0 
0 0 0 0 0 0 7 0 
0 0 0 0 0 0 0 8

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to the number of rows using another Nested For loop.
  • Check if the iterator value of the inner For loop is equal to the iterator value of the parent For loop or not using the If statement.
  • If it is true then print the iterator value of the inner For Loop.
  • Else print 0.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbOfRows+1):
    # Loop from 1 to the number of rows using another Nested For loop.
    for n in range(1, numbOfRows+1):
        # Check if the iterator value of the inner For loop is equal to the iterator value of the parent
        # For loop or not using the If statement.
        if(n == m):
            # If it is true then print n.
            print(n, end=' ')
           # Else print 0.
        else:
            print('0', end=' ')

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

Output:

Enter some random number of rows = 6
1 0 0 0 0 0 
0 2 0 0 0 0 
0 0 3 0 0 0 
0 0 0 4 0 0 
0 0 0 0 5 0 
0 0 0 0 0 6

14) Number Pattern – 14

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from the 1 to the number of rows using For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the space character.
  • Loop from the first loop iterator value to the number of rows(included) using Another For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • Loop from the number of rows -1 to 1(included) in decreasing order using For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the space character.
  • Loop from the first loop iterator value to the number of rows(included) using Another For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 6
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbOfRows+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m):
        # Print the space character.
        print(end=' ')
    # Loop from the first loop iterator value to the number of rows(included)
    # using Another For loop.
    for n in range(m, numbOfRows+1):
        print(n, end=' ')

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

# Loop from the number of rows -1 to 1(included) in decreasing order using For loop.
for m in range(numbOfRows-1, 0, -1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m):
        # Print the space character.
        print(end=' ')
    # Loop from the first loop iterator value to the number of rows(included)
    # using Another For loop.
    for n in range(m, numbOfRows+1):
        print(n, end=' ')

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

Output:

1 2 3 4 5 6 
 2 3 4 5 6 
  3 4 5 6 
   4 5 6 
    5 6 
     6 
    5 6 
   4 5 6 
  3 4 5 6 
 2 3 4 5 6 
1 2 3 4 5 6

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from the 1 to the number of rows using For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the space character.
  • Loop from the first loop iterator value to the number of rows(included) using Another For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • Loop from the number of rows -1 to 1(included) in decreasing order using For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the space character.
  • Loop from the first loop iterator value to the number of rows(included) using Another For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbOfRows+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m):
        # Print the space character.
        print(end=' ')
    # Loop from the first loop iterator value to the number of rows(included)
    # using Another For loop.
    for n in range(m, numbOfRows+1):
        print(n, end=' ')

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

# Loop from the number of rows -1 to 1(included) in decreasing order using For loop.
for m in range(numbOfRows-1, 0, -1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m):
        # Print the space character.
        print(end=' ')
    # Loop from the first loop iterator value to the number of rows(included)
    # using Another For loop.
    for n in range(m, numbOfRows+1):
        print(n, end=' ')

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

Output:

Enter some random number of rows = 8
1 2 3 4 5 6 7 
 2 3 4 5 6 7 
  3 4 5 6 7 
   4 5 6 7 
    5 6 7 
     6 7 
      7 
     6 7 
    5 6 7 
   4 5 6 7 
  3 4 5 6 7 
 2 3 4 5 6 7 
1 2 3 4 5 6 7

15) Number Pattern – 15

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from the 1 to the number of rows using For loop.
  • Loop from 1 to the number of rows -first loop iterator value using another Nested For loop.
  • Print 1.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the iterator value of the parent For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 9
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbOfRows+1):
    # Loop from 1 to the number of rows -first loop iterator
    # value using another Nested For loop.
    for n in range(1, numbOfRows-m+1):
        # Print 1.
        print('1', end=' ')

    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m):
        # Print the iterator value of the parent For loop.
        print(m, end=' ')

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

Output:

1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 2 
1 1 1 1 1 1 3 3 
1 1 1 1 1 4 4 4 
1 1 1 1 5 5 5 5 
1 1 1 6 6 6 6 6 
1 1 7 7 7 7 7 7 
1 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from the 1 to the number of rows using For loop.
  • Loop from 1 to the number of rows -first loop iterator value using another Nested For loop.
  • Print 1.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the iterator value of the parent For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbOfRows+1):
    # Loop from 1 to the number of rows -first loop iterator
    # value using another Nested For loop.
    for n in range(1, numbOfRows-m+1):
        # Print 1.
        print('1', end=' ')

    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m):
        # Print the iterator value of the parent For loop.
        print(m, end=' ')

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

Output:

Enter some random number of rows = 6
1 1 1 1 1 
1 1 1 1 2 
1 1 1 3 3 
1 1 4 4 4 
1 5 5 5 5 
6 6 6 6 6

16) Number Pattern – 16

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from the 1 to the number of rows using For loop.
  • Loop from the iterator value of the parent For loop to the number of rows using another Nested For loop.
  • Print the iterator value of the inner For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 9
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbOfRows+1):
    # Loop from the iterator value of the parent For loop to the number of rows
    # using another Nested For loop.
    for n in range(m, numbOfRows+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')

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

Output:

1 2 3 4 5 6 7 8 9 
2 3 4 5 6 7 8 9 1 
3 4 5 6 7 8 9 1 2 
4 5 6 7 8 9 1 2 3 
5 6 7 8 9 1 2 3 4 
6 7 8 9 1 2 3 4 5 
7 8 9 1 2 3 4 5 6 
8 9 1 2 3 4 5 6 7 
9 1 2 3 4 5 6 7 8

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from the 1 to the number of rows using For loop.
  • Loop from the iterator value of the parent For loop to the number of rows using another Nested For loop.
  • Print the iterator value of the inner For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbOfRows+1):
    # Loop from the iterator value of the parent For loop to the number of rows
    # using another Nested For loop.
    for n in range(m, numbOfRows+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')

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

Output:

Enter some random number of rows = 11
1 2 3 4 5 6 7 8 9 10 11 
2 3 4 5 6 7 8 9 10 11 1 
3 4 5 6 7 8 9 10 11 1 2 
4 5 6 7 8 9 10 11 1 2 3 
5 6 7 8 9 10 11 1 2 3 4 
6 7 8 9 10 11 1 2 3 4 5 
7 8 9 10 11 1 2 3 4 5 6 
8 9 10 11 1 2 3 4 5 6 7 
9 10 11 1 2 3 4 5 6 7 8 
10 11 1 2 3 4 5 6 7 8 9 
11 1 2 3 4 5 6 7 8 9 10

17) Number Pattern – 17

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Take a variable and initialize it with 1 say sampNumber.
  • Loop from 1 to the number of rows of the triangle using For loop.
  • Using another For loop, loop from 1 to the parent loop iterator value (Nested For loop).
  • Inside the inner for loop print the sampNumber with a space character.
  • Increase the value of sampNum by 1.
  • Print the Newline Character after the end of the inner for loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows of the triangle as static input and store it in a variable.
numbOfRows = 4
# Take a variable and initialize it with 1 say sampNumber.
sampNumber = 1
# Loop from 1 to the number of rows of the triangle using For loop.
for m in range(1, numbOfRows+1):
  # Using another For loop, loop from 1 to the parent loop iterator value (Nested For loop).
    for n in range(1, m+1):
        # Inside the inner for loop print the sampNumber with a space character.
        print(sampNumber, end=' ')
        # Increase the value of sampNumber by 1.
        sampNumber = sampNumber+1
    # Print the Newline Character after the end of the inner for loop.
    print()

Output:

1 
2 3 
4 5 6 
7 8 9 10

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Take a variable and initialize it with 1 say sampNumber.
  • Loop from 1 to the number of rows of the triangle using For loop.
  • Using another For loop, loop from 1 to the parent loop iterator value (Nested For loop).
  • Inside the inner for loop print the sampNumber with a space character.
  • Increase the value of sampNum by 1.
  • Print the Newline Character after the end of the inner for loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Take a variable and initialize it with 1 say sampNumber.
sampNumber = 1
# Loop from 1 to the number of rows of the triangle using For loop.
for m in range(1, numbOfRows+1):
  # Using another For loop, loop from 1 to the parent loop iterator value (Nested For loop).
    for n in range(1, m+1):
        # Inside the inner for loop print the sampNumber with a space character.
        print(sampNumber, end=' ')
        # Increase the value of sampNumberby 1.
        sampNumber = sampNumber+1
    # Print the Newline Character after the end of the inner for loop.
    print()

Output:

Enter some random number of rows = 6
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21

18) Number Pattern – 18

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Take a variable and initialize its value with the iterator value of the parent For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the samp value.
  • Modify the samp by samp+number of rows – n, where n is the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 9
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbOfRows+1):
    # Take a variable and initialize its value with the
    # iterator value of the parent For loop.
    samp = m
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # Print the samp value.
        print(samp, end=' ')
        # Modify the samp by samp+number of rows - n,
        # where n is the iterator value of the inner For loop.
        samp = samp+numbOfRows-n
    # Print the newline character after ending of inner For loop.
    print()

Output:

1 
2 10 
3 11 18 
4 12 19 25 
5 13 20 26 31 
6 14 21 27 32 36 
7 15 22 28 33 37 40 
8 16 23 29 34 38 41 43 
9 17 24 30 35 39 42 44 45

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Take a variable and initialize its value with the iterator value of the parent For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the samp value.
  • Modify the samp by samp+number of rows – n, where n is the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbOfRows+1):
    # Take a variable and initialize its value with the
    # iterator value of the parent For loop.
    samp = m
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # Print the samp value.
        print(samp, end=' ')
        # Modify the samp by samp+number of rows - n,
        # where n is the iterator value of the inner For loop.
        samp = samp+numbOfRows-n
    # Print the newline character after ending of inner For loop.
    print()

Output:

Enter some random number of rows = 9
1 
2 10 
3 11 18 
4 12 19 25 
5 13 20 26 31 
6 14 21 27 32 36 
7 15 22 28 33 37 40 
8 16 23 29 34 38 41 43 
9 17 24 30 35 39 42 44 45

19) Number Pattern – 19

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 1 to the number of rows of the triangle using For loop.
  • Take a variable and initialize it with the iterator value of the parent For loop say samp.
  • Loop from iterator value of the Parent For loop to 1(included) in decreasing order using another Nested For loop.
  • Inside the inner for loop print the samp with a space character.
  • Increase the value of samp by the number of rows.
  • Print the Newline Character after the end of the inner for loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 8
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbOfRows+1):
    # Take a variable and initialize it with the iterator value of
    # the parent For loop say samp.
    samp = m
    # Loop from iterator value of the Parent For loop to 1(included)
    # in decreasing order using another Nested For loop.
    for n in range(m, 0, -1):
        # Inside the inner for loop print the samp with a space character.
        print(samp, end=' ')
        # Increase the value of samp by the number of rows.
        samp = samp+numbOfRows
    # Print the newline character after ending of inner For loop.
    print()

Output:

1 
2 10 
3 11 19 
4 12 20 28 
5 13 21 29 37 
6 14 22 30 38 46 
7 15 23 31 39 47 55 
8 16 24 32 40 48 56 64

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from 1 to the number of rows of the triangle using For loop.
  • Take a variable and initialize it with the iterator value of the parent For loop say samp.
  • Loop from iterator value of the Parent For loop to 1(included) in decreasing order using another Nested For loop.
  • Inside the inner for loop print the samp with a space character.
  • Increase the value of samp by the number of rows.
  • Print the Newline Character after the end of the inner for loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbOfRows+1):
    # Take a variable and initialize it with the iterator value of
    # the parent For loop say samp.
    samp = m
    # Loop from iterator value of the Parent For loop to 1(included)
    # in decreasing order using another Nested For loop.
    for n in range(m, 0, -1):
        # Inside the inner for loop print the samp with a space character.
        print(samp, end=' ')
        # Increase the value of samp by the number of rows.
        samp = samp+numbOfRows
    # Print the newline character after ending of inner For loop.
    print()

Output:

Enter some random number of rows = 9
1 
2 11 
3 12 21 
4 13 22 31 
5 14 23 32 41 
6 15 24 33 42 51 
7 16 25 34 43 52 61 
8 17 26 35 44 53 62 71 
9 18 27 36 45 54 63 72 81

20) Number Pattern – 20

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from the number of rows to 1(included) in decreasing order using For loop.
  • Loop from 1 to iterator value of the parent For loop using another Nested For loop.
  • Inside the inner for loop print the space character.
  • Loop from parent loop iterator value to the number of rows using another For loop(Inner For loop).
  • Print the iterator value of the inner For loop.
  • Print the Newline Character after the end of the inner for loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 9
# Loop from the number of rows to 1(included) in decreasing order using For loop.
for m in range(numbOfRows, 0, -1):
    # Loop from 1 to iterator value of the parent For loop using another Nested For loop.
    for n in range(1, m):
        # Inside the inner for loop print the space character.
        print(end=' ')
    # Loop from parent loop iterator value to the number of rows
    # using another For loop(Inner For loop).
    for n in range(m, numbOfRows+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output:

        9 
       8 9 
      7 8 9 
     6 7 8 9 
    5 6 7 8 9 
   4 5 6 7 8 9 
  3 4 5 6 7 8 9 
 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from the number of rows to 1(included) in decreasing order using For loop.
  • Loop from 1 to iterator value of the parent For loop using another Nested For loop.
  • Inside the inner for loop print the space character.
  • Loop from parent loop iterator value to the number of rows using another For loop(Inner For loop).
  • Print the iterator value of the inner For loop.
  • Print the Newline Character after the end of the inner for loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from the number of rows to 1(included) in decreasing order using For loop.
for m in range(numbOfRows, 0, -1):
    # Loop from 1 to iterator value of the parent For loop using another Nested For loop.
    for n in range(1, m):
        # Inside the inner for loop print the space character.
        print(end=' ')
    # Loop from parent loop iterator value to the number of rows
    # using another For loop(Inner For loop).
    for n in range(m, numbOfRows+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output:

Enter some random number of rows = 9
        9 
       8 9 
      7 8 9 
     6 7 8 9 
    5 6 7 8 9 
   4 5 6 7 8 9 
  3 4 5 6 7 8 9 
 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9

21) Number Pattern – 21

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from the first loop iterator value to the number of rows using another Nested For loop.
  • Print the iterator value of the inner For loop.
  • Loop from the number of rows-1 to iterator value of the parent For loop (included) in decreasing order using another For loop(Inner For Loop).
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 9
# Loop from 1 to the number of rows using For Loop.
for m in range(1, numbOfRows+1):
    # Loop from the first loop iterator value
    # to the number of rows using another Nested For loop.
    for n in range(m, numbOfRows+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    '''Loop from the number of rows-1 to iterator value of the parent For loop (included)
    in decreasing order using another For loop(Inner For Loop). '''
    for n in range(numbOfRows-1, m-1, -1):
       # Print the iterator value of the inner For loop.
        print(n, end=' ')

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

Output:

1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 
2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 
3 4 5 6 7 8 9 8 7 6 5 4 3 
4 5 6 7 8 9 8 7 6 5 4 
5 6 7 8 9 8 7 6 5 
6 7 8 9 8 7 6 
7 8 9 8 7 
8 9 8 
9

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from the first loop iterator value to the number of rows using another Nested For loop.
  • Print the iterator value of the inner For loop.
  • Loop from the number of rows-1 to iterator value of the parent For loop (included) in decreasing order using another For loop(Inner For Loop).
  • Print the iterator value of the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For Loop.
for m in range(1, numbOfRows+1):
    # Loop from the first loop iterator value
    # to the number of rows using another Nested For loop.
    for n in range(m, numbOfRows+1):
        # Print the iterator value of the inner For loop.
        print(n, end=' ')
    '''Loop from the number of rows-1 to iterator value of the parent For loop (included)
    in decreasing order using another For loop(Inner For Loop). '''
    for n in range(numbOfRows-1, m-1, -1):
       # Print the iterator value of the inner For loop.
        print(n, end=' ')

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

Output:

Enter some random number of rows = 13
1 2 3 4 5 6 7 8 9 10 11 12 13 12 11 10 9 8 7 6 5 4 3 2 1 
2 3 4 5 6 7 8 9 10 11 12 13 12 11 10 9 8 7 6 5 4 3 2 
3 4 5 6 7 8 9 10 11 12 13 12 11 10 9 8 7 6 5 4 3 
4 5 6 7 8 9 10 11 12 13 12 11 10 9 8 7 6 5 4 
5 6 7 8 9 10 11 12 13 12 11 10 9 8 7 6 5 
6 7 8 9 10 11 12 13 12 11 10 9 8 7 6 
7 8 9 10 11 12 13 12 11 10 9 8 7 
8 9 10 11 12 13 12 11 10 9 8 
9 10 11 12 13 12 11 10 9 
10 11 12 13 12 11 10 
11 12 13 12 11 
12 13 12 
13

22) Number Pattern – 22

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from the number of rows to the iterator value of the parent For loop using another For loop(Inner For loop).
  • Print the space character.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the first loop iterator value.
  • Print the newline character after ending of inner For loop.
  • The Exit of the Program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 9
# Loop from 1 to the number of rows using For Loop.
for m in range(1, numbOfRows+1):
    # Loop from the number of rows to the iterator value of the parent
    # For loop using another For loop(Inner For loop).
    for n in range(numbOfRows, m, -1):
        # Print the space character.
        print(end=' ')
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
       # Print the first loop iterator value.
        print(m, end=' ')

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

Output:

        1 
       2 2 
      3 3 3 
     4 4 4 4 
    5 5 5 5 5 
   6 6 6 6 6 6 
  7 7 7 7 7 7 7 
 8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from the number of rows to the iterator value of the parent For loop using another For loop(Inner For loop).
  • Print the space character.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the first loop iterator value.
  • Print the newline character after ending of inner For loop.
  • The Exit of the Program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For Loop.
for m in range(1, numbOfRows+1):
    # Loop from the number of rows to the iterator value of the parent
    # For loop using another For loop(Inner For loop).
    for n in range(numbOfRows, m, -1):
        # Print the space character.
        print(end=' ')
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
       # Print the first loop iterator value.
        print(m, end=' ')

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

Output:

Enter some random number of rows = 7
      1 
     2 2 
    3 3 3 
   4 4 4 4 
  5 5 5 5 5 
 6 6 6 6 6 6 
7 7 7 7 7 7 7

23) Number Pattern – 23

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Loop from 0 to the number of rows using another For loop(Inner For loop).
  • Check if the inner Loop iterator value is divisible by 2 or not using the If statement.
  • If it is divisible then print (number of rows * n)+m+1 Where m is the iterator value of the parent For loop and n is the iterator value of the inner For loop.
  • Else print (number of rows * (n+1) ) -m.
  • Print the newline character after ending of inner For loop.
  • The Exit of the Program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 4
# Loop from 0 to the number of rows using For loop.
for m in range(0, numbOfRows):
    # Loop from 0 to the number of rows using another For loop(Inner For loop).
    for n in range(0, numbOfRows):
        # Check if the inner Loop iterator value is divisible by 2 or not
        # using the If statement.
        if(n % 2 == 0):
            # If it is divisible then print (number of rows * n)+m+1
            # Where m is the iterator value of the parent For loop
            # and n is the iterator value of the inner For loop.
            print((numbOfRows*n)+m+1, end=' ')
        # Else print (number of rows * (n+1) ) -m.
        else:
            print((numbOfRows * (n+1)) - m, end=' ')

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

Output:

1 8 9 16 
2 7 10 15 
3 6 11 14 
4 5 12 13

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Loop from 0 to the number of rows using another For loop(Inner For loop).
  • Check if the inner Loop iterator value is divisible by 2 or not using the If statement.
  • If it is divisible then print (number of rows * n)+m+1 Where m is the iterator value of the parent For loop and n is the iterator value of the inner For loop.
  • Else print (number of rows * (n+1) ) -m.
  • Print the newline character after ending of inner For loop.
  • The Exit of the Program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 0 to the number of rows using For loop.
for m in range(0, numbOfRows):
    # Loop from 0 to the number of rows using another For loop(Inner For loop).
    for n in range(0, numbOfRows):
        # Check if the inner Loop iterator value is divisible by 2 or not
        # using the If statement.
        if(n % 2 == 0):
            # If it is divisible then print (number of rows * n)+m+1
            # Where m is the iterator value of the parent For loop
            # and n is the iterator value of the inner For loop.
            print((numbOfRows*n)+m+1, end=' ')
        # Else print (number of rows * (n+1) ) -m.
        else:
            print((numbOfRows * (n+1)) - m, end=' ')

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

Output:

Enter some random number of rows = 5
1 10 11 20 21 
2 9 12 19 22 
3 8 13 18 23 
4 7 14 17 24 
5 6 15 16 25

24) Number Pattern – 24

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from the iterator value of the parent For loop to the number of rows using another For loop(Inner For loop).
  • Print the inner loop iterator value.
  • Loop from iterator value of the parent For loop-1 to 1(included) in decreasing order using another Inner For loop.
  • Print the inner loop iterator value.
  • Print the newline character after ending of inner For loop.
  • The Exit of the Program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 7
# Loop from 1 to the number of rows using For Loop.
for m in range(1, numbOfRows+1):
    # Loop from the iterator value of the parent For loop to the
    # number of rows using another For loop(Inner For loop).
    for n in range(m, numbOfRows+1):
        # Print the inner loop iterator value.
        print(n, end=' ')
    # Loop from iterator value of the parent For loop-1 to 1(included)
    # in decreasing order
    # using another Inner For loop.
    for n in range(m-1, 0, -1):
        # Print the inner loop iterator value
        print(n, end=' ')

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

Output:

1 2 3 4 5 6 7 
2 3 4 5 6 7 1 
3 4 5 6 7 2 1 
4 5 6 7 3 2 1 
5 6 7 4 3 2 1 
6 7 5 4 3 2 1 
7 6 5 4 3 2 1

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from the iterator value of the parent For loop to the number of rows using another For loop(Inner For loop).
  • Print the inner loop iterator value.
  • Loop from iterator value of the parent For loop-1 to 1(included) in decreasing order using another Inner For loop.
  • Print the inner loop iterator value.
  • Print the newline character after ending of inner For loop.
  • The Exit of the Program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For Loop.
for m in range(1, numbOfRows+1):
    # Loop from the iterator value of the parent For loop to the
    # number of rows using another For loop(Inner For loop).
    for n in range(m, numbOfRows+1):
        # Print the inner loop iterator value.
        print(n, end=' ')
    # Loop from iterator value of the parent For loop-1 to 1(included)
    # in decreasing order
    # using another Inner For loop.
    for n in range(m-1, 0, -1):
        # Print the inner loop iterator value
        print(n, end=' ')

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

Output:

Enter some random number of rows = 8
1 2 3 4 5 6 7 8 
2 3 4 5 6 7 8 1 
3 4 5 6 7 8 2 1 
4 5 6 7 8 3 2 1 
5 6 7 8 4 3 2 1 
6 7 8 5 4 3 2 1 
7 8 6 5 4 3 2 1 
8 7 6 5 4 3 2 1

25) Number Pattern – 25

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from the number of rows to the iterator value of the parent For loop using another For loop(Inner For loop).
  • Print the space character.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the inner loop iterator value.
  • Print the newline character after ending of inner For loop.
  • The Exit of the Program.

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbOfRows = 9
# Loop from 1 to the number of rows using For Loop.
for m in range(1, numbOfRows+1):
    # Loop from the number of rows to the iterator value of the parent
    # For loop using another For loop(Inner For loop).
    for n in range(numbOfRows, m, -1):
        # Print the space character.
        print(end=' ')
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
       # Print the inner loop iterator value.
        print(n, end=' ')

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

Output:

        1 
       1 2 
      1 2 3 
     1 2 3 4 
    1 2 3 4 5 
   1 2 3 4 5 6 
  1 2 3 4 5 6 7 
 1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 9

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input using int(input()) and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from the number of rows to the iterator value of the parent For loop using another For loop(Inner For loop).
  • Print the space character.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the inner loop iterator value.
  • Print the newline character after ending of inner For loop.
  • The Exit of the Program.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numbOfRows = int(input('Enter some random number of rows = '))
# Loop from 1 to the number of rows using For Loop.
for m in range(1, numbOfRows+1):
    # Loop from the number of rows to the iterator value of the parent
    # For loop using another For loop(Inner For loop).
    for n in range(numbOfRows, m, -1):
        # Print the space character.
        print(end=' ')
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
       # Print the inner loop iterator value.
        print(n, end=' ')

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

Output:

Enter some random number of rows = 13
            1 
           1 2 
          1 2 3 
         1 2 3 4 
        1 2 3 4 5 
       1 2 3 4 5 6 
      1 2 3 4 5 6 7 
     1 2 3 4 5 6 7 8 
    1 2 3 4 5 6 7 8 9 
   1 2 3 4 5 6 7 8 9 10 
  1 2 3 4 5 6 7 8 9 10 11 
 1 2 3 4 5 6 7 8 9 10 11 12 
1 2 3 4 5 6 7 8 9 10 11 12 13

Python Program to Read a File and Capitalize the First Letter of Every Word in the File

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Files in Python:

A file is a piece of information or data that is kept in computer storage devices. You are already familiar with several types of files, such as music files, video files, and text files. Python makes it simple to manipulate these files. In general, files are classified into two types: text files and binary files. Text files are plain text, whereas binary files include binary data that can only be read by a computer.

Python file handling (also known as File I/O) is an important topic for programmers and automation testers. It is necessary to work with files in order to either write to or read data from them.

Furthermore, if you are not already aware, I/O operations are the most expensive procedures through which a program might fail. As a result, you should take caution while implementing file handling for reporting or any other reason. Optimizing a single file activity can contribute to the development of a high-performance application or a solid solution for automated software testing.

Given  a file, thee= task is to read the given file and capitalize the first letter of every word in the given file

Program to Read a File and Capitalize the First Letter of Every Word in the File

Below is the full process Read a File and Capitalize the First Letter of Every Word in the File.

Approach:

  • 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, go over the lines in the first file.
  • To capitalize each word in the line, use the title() function.
  • Print the file’s modified lines.
  • The exit of the program.

Below is the implementation:

# Enter the file name of the first file using the input() function and store it in a variable.
filename = input("Enter the first file name = ")
# In read mode, open the first 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:
      # To capitalize each word in the line, use the title() function.
        line = fileline.title()
        # Print the file's modified lines.
        print(line)

Explanation:

  • A file name needs to be entered by the user.
  • The file is opened in reading mode with the open() function.
  • To read every line in the file, a for loop is needed.
  • The title() method is used to capitalize every word in the line.
  • The modified lines will be printed.

Output:

Before Modifying:

Python is a high-level, object-oriented, interpretable, dynamic semantic programming language.
Its high-level data structures, along with dynamic typing and dynamic connections are particularly 
interesting both for the development of rapid application and for the usage as a script or glue language 
in connecting existing components. The basic, easy syntax of Python underlines readability and hence saves 
software maintenance costs.
Python allows modules and packages, which promote the modularity of the programme and the reuse of code.
The Python interpreter and the vast standard library can be freely supplied, either as a source or as a binary,
for all major platforms without payment.The improved productivity of programmers is often a result of the love of Python.
The edit-test-debug cycle is extraordinarily rapid, given there is no compilation stage. It is easy to debug 
Python programmes: a bug or poor input never causes segmentation errors. Rather, it does raise an exception when the
interpreter discovers an error. The tracer prints a stack track if the programme does not catch the exception.
A source level debugger allows you to check local and global variables, evaluate arbits, set breakpoints, 
line by line code, and so on. A source level debugger is available. The debugger is written in Python, 
showing the introspective power of Python. On the other hand, the most easy way of debugging a programme often is to
add several print statements to the source: this basic approach has a fast editing-test-debug cycle.

After Modifying:

Enter the first file name = samplefile1.txt
Python Is A High-Level, Object-Oriented, Interpretable, Dynamic Semantic Programming Language.
Its High-Level Data Structures, Along With Dynamic Typing And Dynamic Connections Are Particularly
Interesting Both For The Development Of Rapid Application And For The Usage As A Script Or Glue Language
In Connecting Existing Components. The Basic, Easy Syntax Of Python Underlines Readability And Hence Saves
Software Maintenance Costs.
Python Allows Modules And Packages, Which Promote The Modularity Of The Programme And The Reuse Of Code.
The Python Interpreter And The Vast Standard Library Can Be Freely Supplied, Either As A Source Or As A Binary,
For All Major Platforms Without Payment.The Improved Productivity Of Programmers Is Often A Result Of The Love Of Python.
The Edit-Test-Debug Cycle Is Extraordinarily Rapid, Given There Is No Compilation Stage. It Is Easy To Debug
Python Programmes: A Bug Or Poor Input Never Causes Segmentation Errors. Rather, It Does Raise An Exception When The
Interpreter Discovers An Error. The Tracer Prints A Stack Track If The Programme Does Not Catch The Exception.
A Source Level Debugger Allows You To Check Local And Global Variables, Evaluate Arbits, Set Breakpoints,
Line By Line Code, And So On. A Source Level Debugger Is Available. The Debugger Is Written In Python,
Showing The Introspective Power Of Python. On The Other Hand, The Most Easy Way Of Debugging A Programme Often 
Is To Add Several Print Statements To The Source: This Basic Approach Has A Fast Editing-Test-Debug Cycle.

Google Colab Images:

Files and Code:

Code:

Output Image:

Sample file:

Related Programs: