Python Program to Print Alternate Numbers Pattern using While Loop

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

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

Examples:

Example1:

Input:

Given number of rows = 9

Output:

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

Example2:

Input:

Given number of rows = 7

Output:

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

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

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

Method #1: Using While Loop (Static Input)

Approach:

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

1) Python Implementation

Below is the implementation:

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

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

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

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

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

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

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

Output:

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

Method #2: Using While Loop (User Input)

Approach:

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

1) Python Implementation

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

Below is the implementation:

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

Output:

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

2) C++ Implementation

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

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

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

Output:

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

3) C Implementation

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

Below is the implementation:

#include <stdio.h>

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

Output:

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

Related Programs:

Related Programs: