Python Program to Print Even Number Pattern

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

Given the number of rows, the task is to print Even Number Pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows = 10

Output:

20 
20 18 
20 18 16 
20 18 16 14 
20 18 16 14 12 
20 18 16 14 12 10 
20 18 16 14 12 10 8 
20 18 16 14 12 10 8 6 
20 18 16 14 12 10 8 6 4 
20 18 16 14 12 10 8 6 4 2

Example2:

Input:

Given number of rows = 6

Output:

12 
12 10 
12 10 8 
12 10 8 6 
12 10 8 6 4 
12 10 8 6 4 2

Program to Print Even Number Pattern in C, C++, and Python

Below are the ways to print Even Number Pattern 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 initialize it with 2*double the number of rows say lastnumb.
  • Take another variable and initialize it with the lastnumb say evennumb.
  • Loop from 1 to the number of rows using For loop.
  • Initialize the evennumb with the lastnumb.
  • Loop from 0 to the iterator value of the parent For loop using another for loop(Nested For loop).
  • Print the evennumb.
  • Reduce the evennumb by 2.
  • 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 = 10
# Take a variable and initialize it with 2*double the number of rows say lastnumb.
lastnumb = 2*numberOfRows
# Take another variable and initialize it with the lastnumb say evennumb.
evennumb = lastnumb
# Loop from 1 to the number of rows using For loop.
for m in range(1, numberOfRows+1):
    # Initialize the evennumb with the lastnumb.
    evennumb = lastnumb
    # Loop from 0 to the iterator value of the parent For loop using
    # another for loop(Nested For loop).
    for n in range(0, m):
        # Print the evennumb.
        print(evennumb, end=' ')
        # Reduce the evennumb by 2.
        evennumb = evennumb-2

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

Output:

20 
20 18 
20 18 16 
20 18 16 14 
20 18 16 14 12 
20 18 16 14 12 10 
20 18 16 14 12 10 8 
20 18 16 14 12 10 8 6 
20 18 16 14 12 10 8 6 4 
20 18 16 14 12 10 8 6 4 2

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;
    // Take a variable and initialize it with 2*double the
    // number of rows say lastnumb.
    int lastnumb = 2 * numberOfRows;

    // Take another variable and initialize it with the
    // lastnumb say evennumb.
    int evennumb = lastnumb;
    //  Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numberOfRows; m++) {
        // Initialize the evennumb with the lastnumb.
        evennumb = lastnumb;
        // Loop from 0 to the iterator value of the parent
        // For loop using
        // another for loop(Nested For loop).
        for (int n = 0; n < m; n++) {
            // Print the evennumb.
            cout << evennumb << " ";

            // Reduce the evennumb by 2.
            evennumb = evennumb - 2;
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

16 
16 14 
16 14 12 
16 14 12 10 
16 14 12 10 8 
16 14 12 10 8 6 
16 14 12 10 8 6 4 
16 14 12 10 8 6 4 2

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 = 8;
    // Take a variable and initialize it with 2*double the
    // number of rows say lastnumb.
    int lastnumb = 2 * numberOfRows;

    // Take another variable and initialize it with the
    // lastnumb say evennumb.
    int evennumb = lastnumb;
    //  Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numberOfRows; m++) {
        // Initialize the evennumb with the lastnumb.
        evennumb = lastnumb;
        // Loop from 0 to the iterator value of the parent
        // For loop using
        // another for loop(Nested For loop).
        for (int n = 0; n < m; n++) {
            // Print the evennumb.
            printf("%d ", evennumb);

            // Reduce the evennumb by 2.
            evennumb = evennumb - 2;
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

16 
16 14 
16 14 12 
16 14 12 10 
16 14 12 10 8 
16 14 12 10 8 6 
16 14 12 10 8 6 4 
16 14 12 10 8 6 4 2

Method #2: Using For Loop (User Input)

Approach:

  • Give the number of rows as user input and store it in a variable.
  • Take a variable and initialize it with 2*double the number of rows say lastnumb.
  • Take another variable and initialize it with the lastnumb say evennumb.
  • Loop from 1 to the number of rows using For loop.
  • Initialize the evennumb with the lastnumb.
  • Loop from 0 to the iterator value of the parent For loop using another for loop(Nested For loop).
  • Print the evennumb.
  • Reduce the evennumb by 2.
  • 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 = '))
# Take a variable and initialize it with 2*double the number of rows say lastnumb.
lastnumb = 2*numberOfRows
# Take another variable and initialize it with the lastnumb say evennumb.
evennumb = lastnumb
# Loop from 1 to the number of rows using For loop.
for m in range(1, numberOfRows+1):
    # Initialize the evennumb with the lastnumb.
    evennumb = lastnumb
    # Loop from 0 to the iterator value of the parent For loop using
    # another for loop(Nested For loop).
    for n in range(0, m):
        # Print the evennumb.
        print(evennumb, end=' ')
        # Reduce the evennumb by 2.
        evennumb = evennumb-2

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

Output:

Enter some random number of rows = 10
20 
20 18 
20 18 16 
20 18 16 14 
20 18 16 14 12 
20 18 16 14 12 10 
20 18 16 14 12 10 8 
20 18 16 14 12 10 8 6 
20 18 16 14 12 10 8 6 4 
20 18 16 14 12 10 8 6 4 2

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;
    // Take a variable and initialize it with 2*double the
    // number of rows say lastnumb.
    int lastnumb = 2 * numberOfRows;

    // Take another variable and initialize it with the
    // lastnumb say evennumb.
    int evennumb = lastnumb;
    //  Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numberOfRows; m++) {
        // Initialize the evennumb with the lastnumb.
        evennumb = lastnumb;
        // Loop from 0 to the iterator value of the parent
        // For loop using
        // another for loop(Nested For loop).
        for (int n = 0; n < m; n++) {
            // Print the evennumb.
            cout << evennumb << " ";

            // Reduce the evennumb by 2.
            evennumb = evennumb - 2;
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }

    return 0;
}

Output:

6
12 
12 10 
12 10 8 
12 10 8 6 
12 10 8 6 4 
12 10 8 6 4 2 

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);
    // Take a variable and initialize it with 2*double the
    // number of rows say lastnumb.
    int lastnumb = 2 * numberOfRows;

    // Take another variable and initialize it with the
    // lastnumb say evennumb.
    int evennumb = lastnumb;
    //  Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numberOfRows; m++) {
        // Initialize the evennumb with the lastnumb.
        evennumb = lastnumb;
        // Loop from 0 to the iterator value of the parent
        // For loop using
        // another for loop(Nested For loop).
        for (int n = 0; n < m; n++) {
            // Print the evennumb.
            printf("%d ", evennumb);

            // Reduce the evennumb by 2.
            evennumb = evennumb - 2;
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

6
12 
12 10 
12 10 8 
12 10 8 6 
12 10 8 6 4 
12 10 8 6 4 2

Related Programs: