Python Program to Print 1 and 0 in Alternative Rows

Program to Print 1 and 0 in alternative rows in C,C++ and Python

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

Given the number of rows and columns, the task is to print 1 and 0 in alternative rows in C, C++, and Python.

Examples:

Example1:

Input:

given number of rows =4
given number of columns=3

Output:

1 1 1 
0 0 0 
1 1 1 
0 0 0

Example2:

Input:

given number of rows =7
given number of columns=15

Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Program to Print 1 and 0 in Alternative Rows in C, C++, and Python

Below are the ways to print 1 and 0 in alternative rows in C, C++, and Python.

If you look closely at the pattern, you’ll see that for all odd rows, 1 is printed and for all even rows, 0 is printed. As a

result, before displaying integers inside the inner loop, you must check for even-odd conditions. If the current row is

an odd number, print 1 otherwise, print 0.

The following is a step-by-step description of the logic used to print a 1, 0 number pattern at alternate rows.

Method #1: Using For loop

Approach:

  • Give the number of rows and number of columns as static input.
  • Store them in two separate variables row numbers and column numbers.
  • Run an outer loop from 1 to rows to iterate through the rows using For loop.
  • Iterate through the columns from 1 to cols using another For inner loop.
  • Before printing any number, we must first check the condition inside the inner loop.
  • This means that for every odd row, 1 is displayed, and for every even row, 0 is displayed.
  • We check whether the row is odd or not using the if statement.
  • If it is true then print 1 else print 0.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows and number of columns as static input.
# Store them in two separate variables row numbers and column numbers.
rownumbs = 15
colnumbs = 11
# Run an outer loop from 1 to rows to iterate through the rows using For loop.
for m in range(1, rownumbs+1):
  # Iterate through the columns from 1 to cols using another For inner loop.
    for n in range(1, colnumbs+1):
      # Before printing any number, we must first check the condition inside the inner loop.
      # This means that for every odd row, 1 is displayed, and for every even row, 0 is displayed.
          # We check whether the row is odd or not using the if statement.
      # If it is true then print 1 else print 0.
        if(m % 2 == 1):
            print('1', end=' ')
        # If it is true then print 1 else print 0.
        else:
            print('0', end=' ')
    print()

Output:

1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1

2) C++ Implementation

It is the same as the python approach but just the change in syntax.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows and number of columns as
    // static input.
    // Store them in two separate variables row numbers and
    // column numbers.
    int rownumbs = 15;
    int colnumbs = 11;
    // Run an outer loop from 1 to rows to iterate through
    // the rows using For loop.
    for (int i = 1; i <= rownumbs; i++) {
        // Iterate through the columns from 1 to cols using
        // another For inner loop.
        for (int j = 1; j <= colnumbs; j++) {
            // Before printing any number, we must
            // first check the condition inside the
            // inner loop.
            // This means that for every odd row, 1 is
            // displayed, and for every even row,
            // 0 is displayed.
            // We check whether the row is odd or not using
            // the if statement.
            // If it is true then print 1 else print 0.

            if (i % 2 == 1)
                cout << "1 ";
            // If it is true then print 1 else print 0.
            else
                cout << "0 ";
        }
        cout << endl;
    }
    return 0;
}

Output:

1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1

3) C Implementation

It is the same as the python approach but just the change in syntax.

Below is the implementation:

#include <stdio.h>

int main(void)
{
    // Give the number of rows and number of columns as
    // static input.
    // Store them in two separate variables row numbers and
    // column numbers.
    int rownumbs = 15;
    int colnumbs = 11;
    // Run an outer loop from 1 to rows to iterate through
    // the rows using For loop.
    for (int i = 1; i <= rownumbs; i++) {
        // Iterate through the columns from 1 to cols using
        // another For inner loop.
        for (int j = 1; j <= colnumbs;
             j++) { // Before printing any number, we must
            // first check the condition inside the
            // inner loop.
            // This means that for every odd row, 1 is
            // displayed, and for every even row, 0 is
            // displayed.
            /*We check whether the row is odd or not using
              the if statement. If it is true then print 1
              else print 0.*/
            if (i % 2 == 1)
                printf("1 ");
            // If it is true then print 1 else print 0.
            else
                printf("0 ");
        }
        printf("\n");
    }
    return 0;
}

Output:

1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1

Method #2: Using while loop

1) C Implementation

We just iterate till rownumbs and columumbs using a while loop.

Below is the implementation:

#include <stdio.h>

int main(void)
{
    // Give the number of rows and number of columns as
    // static input.
    // Store them in two separate variables row numbers and
    // column numbers.
    int rownumbs = 4;
    int colnumbs = 3;
    // Run an outer loop from 1 to rows to iterate through
    // the rows using For loop.
    int temprow = 1;
    while (temprow <= rownumbs) {
        // Iterate through the columns from 1 to cols using
        // another For inner loop.
        int tempcol = 1;
        while (tempcol <= colnumbs) { // Before printing any
                                      // number, we must
            // first check the condition inside the
            // inner loop. This means that for every odd
            // row, 1 is displayed, and for every even row,
            // 0 is displayed. # We check whether the row is
            // odd or not using the if statement. # If it is
            // true then print 1 else print 0.
            if (temprow % 2 == 1)
                printf("1 ");
            // If it is true then print 1 else print 0.
            else
                printf("0 ");
            tempcol++;
        }
        temprow++;
        printf("\n");
    }
    return 0;
}

Output:

1 1 1 
0 0 0 
1 1 1 
0 0 0

2) C++ Implementation:

We just iterate till rownumbs and columumbs using a while loop.

Below is the implementation:

#include <iostream>
using namespace std;

int main(void)
{
    // Give the number of rows and number of columns as
    // static input.
    // Store them in two separate variables row numbers and
    // column numbers.
    int rownumbs = 2;
    int colnumbs = 19;
    // Run an outer loop from 1 to rows to iterate through
    // the rows using For loop.
    int temprow = 1;
    while (temprow <= rownumbs) {
        // Iterate through the columns from 1 to cols using
        // another For inner loop.
        int tempcol = 1;
        while (tempcol <= colnumbs) { // Before printing any
                                      // number, we must
            // first check the condition inside the
            // inner loop. This means that for every odd
            // row, 1 is displayed, and for every even row,
            // 0 is displayed. # We check whether the row is
            // odd or not using the if statement. # If it is
            // true then print 1 else print 0.
            if (temprow % 2 == 1)
                cout << "1 ";
            // If it is true then print 1 else print 0.
            else
                cout << "0 ";
            tempcol++;
        }
        temprow++;
        cout << endl;
    }
    return 0;
}

Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

3) Python Implementation

We just iterate till rownumbs and columumbs using a while loop.

Below is the implementation:

# Give the number of rows and number of columns as static input.
# Store them in two separate variables row numbers and column numbers.
rownumbs = 6
colnumbs = 11
# Run an outer loop from 1 to rows to iterate through the rows using For loop.
temprow = 1
while(temprow <= rownumbs):
  # Iterate through the columns from 1 to cols using another For inner loop.
    tempcol = 1
    while(tempcol <= colnumbs):
      # Before printing any number, we must first check the condition inside the inner loop.
      # This means that for every odd row, 1 is displayed, and for every even row, 0 is displayed.
      # We check whether the row is odd or not using the if statement.
        if(temprow % 2 == 1):
            print('1', end=' ')
        # If it is true then print 1 else print 0.
        else:
            print('0', end=' ')
        tempcol += 1
    temprow += 1
    print()

Output:

1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0

Related Programs:

Python Program to Print Hollow Rhombus Star Pattern

Python Program to Print Hollow Rhombus Using For Loop Star Pattern

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

Given the number of rows of the rhombus, the task is to print the hollow rhombus Star Pattern in C, C++, and Python.

Examples:

Example1:

Input:

given number of rows of rhombus =11

Output:

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

Example2:

Input:

given number of rows of rhombus =6
given character to print =<

Output:

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

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

Below are the ways to print Hollow Rhombus star patterns in C, C++, and Python.

Method #1: Using For Loop (Star Character)

Approach:

  • Give the number of rows of the rhombus as static input and store it in a variable.
  • Using Nested For loops print the rhombus star pattern.
  • We use the If Else statement to check If the side length of the rhombus(rows) is 0 or maximum – 1.
  • If it is true then print * else print space.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

Python Program to Print Hollow Rhombus Star Pattern

# Give the number of rows of the rhombus as static input and store it in a variable.
rhombusrows = 11
# Using Nested For loops print the rhombus star pattern.
for m in range(rhombusrows, 0, -1):
    for n in range(1, m):
        print(' ', end='')
    for k in range(0, rhombusrows):
      # We use the If Else statement to check If the side length of the rhombus(rows) is 0 or maximum – 1.
       # If it is true then print * else print space.
        if(m == 1 or m == rhombusrows or k == 0 or k == rhombusrows - 1):
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()

Output:

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

2) C++Implementation

Below is the implementation:

CPP Program to Print Hollow Rhombus Using For Loop Star Pattern

#include <iostream>
using namespace std;

int main(void)
{
    // Give the number of rows of the rhombus as static
    // input and store it in a variable.
    int rhombusrows = 6;
    // Using Nested For loops print the rhombus star
    // pattern.
    for (int m = rhombusrows; m > 0; m--) {
        for (int n = 1; n < m; n++) {
            cout << "  ";
        }
        for (int k = 0; k < rhombusrows; k++) {
            // We use the If Else statement to check If the
            // side length of the rhombus(rows) is 0 or
            // maximum – 1. If it is true then print * else
            // print space.
            if (m == 1 || m == rhombusrows || k == 0
                || k == rhombusrows - 1)
            cout << "* ";
            else
                cout << "  ";
        }
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

C Program to Print Hollow Rhombus Using For Loop Star Pattern

#include <stdio.h>

int main()
{
    // Give the number of rows of the rhombus as static
    // input and store it in a variable.
    int rhombusrows = 9;
    // Using Nested For loops print the rhombus star
    // pattern.
    for (int m = rhombusrows; m > 0; m--) {
        for (int n = 1; n < m; n++) {
            printf("  ");
        }
        for (int k = 0; k < rhombusrows; k++) {
            // We use the If Else statement to check If the
            // side length of the rhombus(rows) is 0 or
            // maximum – 1. If it is true then print * else
            // print space.
            if (m == 1 || m == rhombusrows || k == 0
                || k == rhombusrows - 1)
                printf("* ");
            else
                printf("  ");
        }
        printf("\n");
    }
}

Output:

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

Method #2: Using For Loop (User Character)

Approach:

  • Give the number of rows of the rhombus as static input and store it in a variable.
  • Scan the character to print as user input and store it in a variable.
  • Using Nested For loops print the rhombus star pattern.
  • We use the If Else statement to check If the side length of the rhombus(rows) is 0 or maximum – 1.
  • If it is true then print * else print space.
  • The Exit of the Program.

1) Python Implementation

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

Below is the implementation:

D:\Content\Shivam\selenium\Py Images\Python Program to Print Hollow Rhombus Using For Loop User Character.png

# Give the number of sides of the rhombus as user input using int(input()) and store it in a variable.
# Give the Element as user input using int(input()) and store it in another variable.
rhombusrows = int(input('Enter some random number of rows of the rhombus= '))
characte = input('Enter some random character to print = ')
# Using Nested For loops print the rhombus star pattern.
for m in range(rhombusrows, 0, -1):
    for n in range(1, m):
        print(' ', end='')
    for k in range(0, rhombusrows):
      # We use the If Else statement to check If the side length of the rhombus(rows) is 0 or maximum – 1.
       # If it is true then print * else print space.
        if(m == 1 or m == rhombusrows or k == 0 or k == rhombusrows - 1):
            print(characte, end=' ')
        else:
            print(' ', end=' ')
    print()

Output:

Enter some random number of rows of the rhombus= 6
Enter some random character to print = <
          < < < < < < 
        <               < 
      <               < 
    <               < 
  <               < 
< < < < < <

2) C++Implementation

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

Below is the implementation:

CPP Program to Print Hollow Rhombus Using For Loop User Character

#include <iostream>
using namespace std;

int main(void)
{
    int rhombusrows;
    char characte;
    // Give the number of rows of the rhombus as user input
    // using cin and
    // store it in a variable.
    cout << "Enter some random number of rows of the "
            "rhombus = "
         << endl;
    cin >> rhombusrows;
    // Create a character variable.
    // Give the character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character to print = "
         << endl;
    cin >> characte;
    cout << endl;
    for (int m = rhombusrows; m > 0; m--) {
        for (int n = 1; n < m; n++) {
            cout << "  ";
        }
        for (int k = 0; k < rhombusrows; k++) {
            // We use the If Else statement to check If the
            // side length of the rhombus(rows) is 0 or
            // maximum – 1. If it is true then print * else
            // print space.
            if (m == 1 || m == rhombusrows || k == 0
                || k == rhombusrows - 1)
                cout << characte << " ";
            else
                cout << "  ";
        }
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the rhombus = 
5
Enter some random character to print = 
?
    ? ? ? ? ? 
   ?         ? 
  ?         ? 
 ?         ? 
? ? ? ? ?

3) C Implementation

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

Below is the implementation:

D:\Content\Shivam\selenium\Py Images\Python Program to Print Hollow Rhombus Using For Loop User Character.png

#include <stdio.h>

int main()
{
    int rhombusrows;
    char characte;
    // Give the number of rows of the rhombus as user input
    // using scanf and
    // store it in a variable.
    // Create a character variable.
    // Give the character as user input using scanf and
    // store it in another variable.
    // Using Nested For loops print the rhombus pattern.
    scanf("%d%c", &rhombusrows, &characte);
    printf("\n");
    // Using Nested For loops print the rhombus star
    // pattern.
    for (int m = rhombusrows; m > 0; m--) {
        for (int n = 1; n < m; n++) {
            printf("  ");
        }
        for (int k = 0; k < rhombusrows; k++) {
            // We use the If Else statement to check If the
            // side length of the rhombus(rows) is 0 or
            // maximum – 1. If it is true then print * else
            // print space.
            if (m == 1 || m == rhombusrows || k == 0
                || k == rhombusrows - 1)
                printf("%c ", characte);
            else
                printf("  ");
        }
        printf("\n");
    }
}

Output:

5 :
    : : : : : 
   :       : 
  :       : 
 :       : 
: : : : :

Related Programs:

Python Program to Print Alternate Numbers Pattern using While Loop

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

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

Examples:

Example1:

Input:

Given number of rows = 9

Output:

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

Example2:

Input:

Given number of rows = 7

Output:

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

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

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

Method #1: Using While Loop (Static Input)

Approach:

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

1) Python Implementation

Below is the implementation:

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

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

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

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

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

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

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

Output:

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

Method #2: Using While Loop (User Input)

Approach:

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

1) Python Implementation

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

Below is the implementation:

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

Output:

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

2) C++ Implementation

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

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

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

Output:

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

3) C Implementation

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

Below is the implementation:

#include <stdio.h>

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

Output:

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

Related Programs:

Related Programs:

Python Program to Print Floyd’s Triangle

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

Given the number of rows of the triangle, the task is to print Floyd’s triangle in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows of the triangle = 10

Output:

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

Example2:

Input:

Given number of rows of the triangle = 13

Output:

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 36 
37 38 39 40 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 55 
56 57 58 59 60 61 62 63 64 65 66 
67 68 69 70 71 72 73 74 75 76 77 78 
79 80 81 82 83 84 85 86 87 88 89 90 91

Program to Print Floyd’s Triangle in C, C++, and Python

Below are the ways to print Floyd’s triangle in C, C++, and Python.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number of rows of the triangle as static input and store it in a variable.
  • Take a variable and initialize it with 1 say sampNum.
  • 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 sampNum 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.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the triangle as static input and store it in a variable.
triRows = 10
# Take a variable and initialize it with 1 say sampNum.
sampNum = 1
# Loop from 1 to the number of rows of the triangle using For loop.
for m in range(1, triRows+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 sampNum with a space character.
        print(sampNum, end=' ')
        # Increase the value of sampNum by 1.
        sampNum = sampNum+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 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 36 
37 38 39 40 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 55

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the triangle as static
    // input and store it in a variable.
    int triRows = 10;
    // Take a variable and initialize it with 1 say sampNum.
    int sampNum = 1;
    // Loop from 1 to the number of rows of the triangle
    // using For loop.
    for (int m = 1; m <= triRows; m++) {
        for (int n = 1; n <= m; n++) {
            // Inside the inner for loop print the
            // sampNum with a space character.
            cout << sampNum << " ";
            // Increase the value of sampNum by 1.
            sampNum = sampNum + 1;
        }

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

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the triangle as static
    // input and store it in a variable.
    int triRows = 10;
    // Take a variable and initialize it with 1 say sampNum.
    int sampNum = 1;
    // Loop from 1 to the number of rows of the triangle
    // using For loop.
    for (int m = 1; m <= triRows; m++) {
        for (int n = 1; n <= m; n++) {
            // Inside the inner for loop print the
            // sampNum with a space character.
            printf("%d ", sampNum);
            // Increase the value of sampNum by 1.
            sampNum = sampNum + 1;
        }

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

Output:

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

Method #2: Using For Loop (User Input)

Approach:

  • Give the number of rows of the triangle as user input and store it in a variable.
  • Take a variable and initialize it with 1 say sampNum.
  • 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 sampNum 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.

1) Python Implementation

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

Below is the implementation:

# Give the number of rows of the triangle as user input
# using the int(input()) function and store it in a variable.
triRows = int(input('Enter some random number of rows of the triangle = '))
# Take a variable and initialize it with 1 say sampNum.
sampNum = 1
# Loop from 1 to the number of rows of the triangle using For loop.
for m in range(1, triRows+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 sampNum with a space character.
        print(sampNum, end=' ')
        # Increase the value of sampNum by 1.
        sampNum = sampNum+1
    # Print the Newline Character after the end of the inner for loop.
    print()

Output:

Enter some random number of rows of the triangle = 11
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 36 
37 38 39 40 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 55 
56 57 58 59 60 61 62 63 64 65 66 

2) C++ Implementation

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

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the triangle as user input
    // using the cin function and store it in a variable.
    int triRows;
    cout<<"Enter some random number of rows of the triangle = ";
    cin >> triRows;

    // Take a variable and initialize it with 1 say sampNum.
    int sampNum = 1;
    // Loop from 1 to the number of rows of the triangle
    // using For loop.
    for (int m = 1; m <= triRows; m++) {
        for (int n = 1; n <= m; n++) {
            // Inside the inner for loop print the
            // sampNum with a space character.
            cout << sampNum << " ";
            // Increase the value of sampNum by 1.
            sampNum = sampNum + 1;
        }

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

Output:

Enter some random number of rows of the triangle = 13
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 36 
37 38 39 40 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 55 
56 57 58 59 60 61 62 63 64 65 66 
67 68 69 70 71 72 73 74 75 76 77 78 
79 80 81 82 83 84 85 86 87 88 89 90 91

3) C Implementation

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

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the triangle as user input
    // using the scanf function and store it in a variable.
    int triRows;
    scanf("%d", &triRows);
    // Take a variable and initialize it with 1 say sampNum.
    int sampNum = 1;
    // Loop from 1 to the number of rows of the triangle
    // using For loop.
    for (int m = 1; m <= triRows; m++) {
        for (int n = 1; n <= m; n++) {
            // Inside the inner for loop print the
            // sampNum with a space character.
            printf("%d ", sampNum);
            // Increase the value of sampNum by 1.
            sampNum = sampNum + 1;
        }

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

Output:

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

Related Programs:

Python Program to Find the Factors of a Number | C++ Program to Find Factors

Program to Find the Factors of a Number in Python and C++ Programming

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

Factors of a number:

When two whole numbers are multiplied, the result is a product. The factors of the product are the numbers we multiply.

In mathematics, a factor is a number or algebraic expression that equally divides another number or expression, leaving no remainder.

The prime number is defined as a number that has only two factors one and itself. Composite numbers are those that contain more than two variables.

Given a number the task is to print the factors of the given number.

Examples:

Example1:

Input:

given_number=360

Output:

printing the factors of given number
1
2
3
4
5
6
8
9
10
12
15
18
20
24
30
36
40
45
60
72
90
120
180
360

Example2:

Input:

given_number=10

Output:

printing the factors of given number
1
2
5
10

Program to Find the Factors of a Number

Below are the ways to find the factors of a number:

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

1)Using for loop to loop from 1 to N in Python

Algorithm:

Divide a positive number “N” by the natural numbers 1 to “N” to find the factor. If a number is divisible by a natural number, the factor is the natural number. A number N can only have factors in the range of 1 to N.

Approach:

  • Assume the input is a number N.
  • Create an iterator variable and set its value to 1.
  • Using an iterator variable to divide the number N
  • It is a factor of the given number N if it is divisible.
  • The iterator variable should be increased.
  • Rep steps 4 and 5 until the iterator variable reaches the value N.

This is the simplest and most straightforward way to find variables in a Python number programme. While declaring the variables, we’ll take a number. Python software that uses a for-loop to find factors of a number and print the factors on the screen.

Below is the implementation:

# given number
number = 360
# printing the factors of given number
print("printing the factors of given number : ")
# using for loop
for i in range(1, number+1):
    # checking if iterator divides the number if so then print it(because it is factor)
    if(number % i == 0):
        print(i)

Output:

printing the factors of given number : 
1
2
3
4
5
6
8
9
10
12
15
18
20
24
30
36
40
45
60
72
90
120
180
360

2)Using for loop to loop from 1 to N in C++

It is similar to method 1 except the syntax. Let us see how to find factors  using for loop in c++

Below is the implementation:

#include <iostream>
using namespace std;
int main()
{ // given number
    int number = 360;
    // printing the factors
    cout << "printing the factors of given number :"
         << endl;
    // using for loop to loop from 1 to N
    for (int i = 1; i <= number; i++)
        // checking if iterator divides the number if so
        // then print it(because it is factor)
        if (number % i == 0)
            cout << i << endl;
    return 0;
}

Output:

printing the factors of given number :
1
2
3
4
5
6
8
9
10
12
15
18
20
24
30
36
40
45
60
72
90
120
180
360

We can see that the outputs of both the programs are same

3)Limitations of running loop from 1 to N

In these two methods the loop runs from 1 to number N.

Hence we can say that the time complexity of above methods are O(n).

What if the number is very large?

Like 10^18 the above methods takes nearly 31 years to execute.

Then How to avoid this?

We can see that the factors of the numbers exist from 1 to N/2 except number itself.

But this also takes nearly 15 yrs to execute.

So to above this we loop till square root of N in next method which gives Time Complexity O(Sqrt(n)).

4)Using while loop to loop from 1 to SQRT(N) in Python

Many of the divisors are present in pairs if we look closely. For eg, if n = 16, the divisors are (1,16), (2,8), and (3,8), (4,4)
We could significantly speed up our program if we took advantage of this fact.
However, if there are two equal divisors, as in the case of, we must be cautious (4, 4). In that case, we’d just print one of them.

We use while loop to loop from 1 to sqrt(number)

Below is the implementation:

# importing math module
import math
# given number
number = 360
# taking a iterator and initlaizing it with 1
i = 1
print("printing the factors of given number : ")
# looping till sqrt(number) using while
while i <= math.sqrt(number):

    if (number % i == 0):

        # If both thee divisors are equal then print only one divisor
        if (number / i == i):
            print(i)
        else:
            # else print both the divisors
            print(i)
            print(number//i)
   # increment the iterator by 1
    i += 1

Output:

printing the factors of given number : 
1
360
2
180
3
120
4
90
5
72
6
60
8
45
9
40
10
36
12
30
15
24
18
20

Related Programs:

Python Program to Print Square Pattern with Numbers

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

Given the number of rows, the task is to print Square Pattern with Numbers in C, C++, and Python

Examples:

Example1:

Input:

Given number of rows = 9

Output:

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

Example2:

Input:

Given number of rows = 6

Output:

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

Program to Print Square Pattern with Numbers in C, C++, and Python

Below are the ways to print Square Pattern with Numbers 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 rows using another for loop(Nested For loop).
  • Check if the iterator value of the inner For loop is less than or equal to the parent loop iterator value of theFor Loop using the If conditional Statement.
  • If it is true then print the iterator value of the parent For loop.
  • Else 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.

1) Python Implementation

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numbrrows = 9
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbrrows+1):
    # Loop from 1 to the number of rows using another for loop(Nested For loop).
    for n in range(1, numbrrows+1):
        ''' Check if the iterator value of the inner For loop is less than or equal 
            to the parent loop iterator value of the
            For Loop using the If conditional Statement.'''
        if(n <= m):
            # If it is true then print the iterator value of the parent For loop.
            print(m, end=' ')
        else:
            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 
2 2 3 4 5 6 7 8 9 
3 3 3 4 5 6 7 8 9 
4 4 4 4 5 6 7 8 9 
5 5 5 5 5 6 7 8 9 
6 6 6 6 6 6 7 8 9 
7 7 7 7 7 7 7 8 9 
8 8 8 8 8 8 8 8 9 
9 9 9 9 9 9 9 9 9

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 = 5;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numbrrows; m++) {
        // Loop from 1 to the number of rows using another
        // for loop(Nested For loop).
        for (int n = 1; n <= numbrrows; n++) {
            /*Check if the iterator value of the inner For
            loop is less than or equal to the parent loop
            iterator value of the For Loop using the If
            conditional Statement.*/
            if (n <= m)
                // If it is true then print the iterator
                // value of the parent For loop.
                cout << m << " ";
            else
                cout << n << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }
    return 0;
}

Output:

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

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 = 3;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numbrrows; m++) {
        // Loop from 1 to the number of rows using another
        // for loop(Nested For loop).
        for (int n = 1; n <= numbrrows; n++) {
            /*Check if the iterator value of the inner For
            loop is less than or equal to the parent loop
            iterator value of the For Loop using the If
            conditional Statement.*/
            if (n <= m)
                // If it is true then print the iterator
                // value of the parent For loop.
                printf("%d ", m);
            else
                printf("%d ", n);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

1 2 3 
2 2 3 
3 3 3

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 rows using another for loop(Nested For loop).
  • Check if the iterator value of the inner For loop is less than or equal to the parent loop iterator value of theFor Loop using the If conditional Statement.
  • If it is true then print the iterator value of the parent For loop.
  • Else 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.

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 = '))
# Loop from 1 to the number of rows using For loop.
for m in range(1, numbrrows+1):
    # Loop from 1 to the number of rows using another for loop(Nested For loop).
    for n in range(1, numbrrows+1):
        ''' Check if the iterator value of the inner For loop is less than or equal 
            to the parent loop iterator value of the
            For Loop using the If conditional Statement.'''
        if(n <= m):
            # If it is true then print the iterator value of the parent For loop.
            print(m, end=' ')
        else:
            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 3 4 5 6 7 
2 2 3 4 5 6 7 
3 3 3 4 5 6 7 
4 4 4 4 5 6 7 
5 5 5 5 5 6 7 
6 6 6 6 6 6 7 
7 7 7 7 7 7 7

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;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numbrrows; m++) {
        // Loop from 1 to the number of rows using another
        // for loop(Nested For loop).
        for (int n = 1; n <= numbrrows; n++) {
            /*Check if the iterator value of the inner For
            loop is less than or equal to the parent loop
            iterator value of the For Loop using the If
            conditional Statement.*/
            if (n <= m)
                // If it is true then print the iterator
                // value of the parent For loop.
                cout << m << " ";
            else
                cout << n << " ";
        }
        // Print the Newline character after the end of the
        // inner loop.
        cout << endl;
    }
    return 0;
}

Output:

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

3) C Implementation

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

Below is the implementation:

#include <math.h>
#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);
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= numbrrows; m++) {
        // Loop from 1 to the number of rows using another
        // for loop(Nested For loop).
        for (int n = 1; n <= numbrrows; n++) {
            /*Check if the iterator value of the inner For
            loop is less than or equal to the parent loop
            iterator value of the For Loop using the If
            conditional Statement.*/
            if (n <= m)
                // If it is true then print the iterator
                // value of the parent For loop.
                printf("%d ", m);
            else
                printf("%d ", n);
        }
        // Print the Newline character after the end of the
        // inner loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Related Programs:

Python Program to Display Powers of 2 till N

Program To Display Powers of 2 till N

Given the Number N , the task is to print the powers of 2 till N.

Examples:

Example1:

Input:

Number = 10

Output:

The total terms of the number = 10
Value of 2 power 0 = 1
Value of 2 power 1 = 2
Value of 2 power 2 = 4
Value of 2 power 3 = 8
Value of 2 power 4 = 16
Value of 2 power 5 = 32
Value of 2 power 6 = 64
Value of 2 power 7 = 128
Value of 2 power 8 = 256
Value of 2 power 9 = 512

Example2:

Input:

Number = 20

Output:

The total terms of the number = 20
Value of 2 power 0 = 1
Value of 2 power 1 = 2
Value of 2 power 2 = 4
Value of 2 power 3 = 8
Value of 2 power 4 = 16
Value of 2 power 5 = 32
Value of 2 power 6 = 64
Value of 2 power 7 = 128
Value of 2 power 8 = 256
Value of 2 power 9 = 512
Value of 2 power 10 = 1024
Value of 2 power 11 = 2048
Value of 2 power 12 = 4096
Value of 2 power 13 = 8192
Value of 2 power 14 = 16384
Value of 2 power 15 = 32768
Value of 2 power 16 = 65536
Value of 2 power 17 = 131072
Value of 2 power 18 = 262144
Value of 2 power 19 = 524288

Program To Display Powers of 2 till N in Python

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1:Using ** operator

We can calculate the power of the 2 of the given number using ** operator.

We can take a loop from 0 to number and print the power of 2 of the given iterator value.

Print the power of 2 of given iterator value.

Below is the implementation:

# given number n
number = 10
# printing the power of 2 of the iterator value
for i in range(number):
    # calculating the power_of_2 of i
    powvalue = 2**i
    print("Value of 2 power", i, "=", powvalue)

Output:

Value of 2 power 0 = 1
Value of 2 power 1 = 2
Value of 2 power 2 = 4
Value of 2 power 3 = 8
Value of 2 power 4 = 16
Value of 2 power 5 = 32
Value of 2 power 6 = 64
Value of 2 power 7 = 128
Value of 2 power 8 = 256
Value of 2 power 9 = 512

Method #2:Using Anonymous function in Python

To determine the powers of 2 in the program below, we utilized the anonymous (lambda) function inside the map() built-in function. In Python, an anonymous function is one that is not given a name.
The def keyword is used to define conventional functions, whereas the lambda keyword is used to define anonymous functions in Python. As a result, anonymous functions are referred to as lambda functions.

Syntax:

lambda arguments: expression

Lambda functions can take any number of parameters but can only execute one expression.The result is returned once the expression has been evaluated.

Below is the implementation:

# Using the anonymous function, display the powers of two.
# given number n
number = 10
# use anonymous function to print powers of 2 till given number
resultTerms = list(map(lambda x: 2 ** x, range(number)))
print("The total terms of the number = ", number)
# print the powers of 2 till number
for i in range(number):
    print("Value of 2 power", i, "=", resultTerms[i])

Output:

The total terms of the number = 10
Value of 2 power 0 = 1
Value of 2 power 1 = 2
Value of 2 power 2 = 4
Value of 2 power 3 = 8
Value of 2 power 4 = 16
Value of 2 power 5 = 32
Value of 2 power 6 = 64
Value of 2 power 7 = 128
Value of 2 power 8 = 256
Value of 2 power 9 = 512

Explanation:

The following statement appears in the above program:

result = lambda x: 2 ** x specifies that any value we pass to result later is transferred to x, and 2 ** x is returned. result (3) returns 2 ** 3 or 2*2*2 or 8 in this case.
To make a list, use the list() function. After applying the function to each item of a provided iterable (in the example of the previous application, a list), map() produces an iterator of results.

Related Programs:

What is Hashing and Hash Table?

What is Hashing and Hash Table

Hashing and Hash Table

1)Hashing

Hashing is the process of mapping object data to a representative integer value using a function or algorithm.

This hash code (or simply hash) can then be used to narrow our quest when searching for the item on the map.

These hash codes are usually used to create an index at which the value is stored.

2)Hash Table

A hash table is a data structure that stores data associatively. Data is stored in an array format in a hash table, with each data value having its own unique index value. When we know the index of the desired data, we can access it very quickly.

As a result, it becomes a data structure in which insertion and search operations are extremely quick, regardless of the size of the data. Hash Tables use an array as a storage medium and use the hash technique to produce an index from which an element is to be inserted or located.

3)Features of HashTable

  • It works in the same way as HashMap, but it is synchronised.
  • In a hash table, a key/value pair is stored.
  • In Hashtable, we define an object that will be used as a key, as well as the value that will be associated with that key. The key is then hashed, and the resulting hash code serves as the index for storing the value in the table.
  • The default capacity of the Hashtable class is 11, and the loadFactor is 0.75.
  • HashMap does not support Enumeration, while Hashtable does not support fail-fast Enumeration.

4)Adding element in HashTable

When an entity is inserted into a Hash Table, its hash code is measured first, and the bucket in which it will be stored is determined based on that hash code.

Let us illustrate this with an example.

For instance, suppose we want to store some numbers in a Hash Table, i.e.

32 , 45 , 64 , 92 , 57 , 88 , 73

Internally, this Hash Table can use 10 buckets, i.e.

The Hash Code will be returned by our Hash Function.

HashCode=Element_value%10

This Hash code for,

32 will be 2

45 will be 5

64 will be 4

92 will be 2

57 will be 7

88 will be 8

73 will be 3

Now, each element’s hash code will be used to determine where that element will be stored, for example, 45 will be stored in bucket 5 since its hash code is 5. In the same way, all elements will be stored in the bucket that corresponds to their hash code.

5)Hashing Collisions

As we can see, the hash code for both 32 and 92 is the same, which is 2. In Hashing, this is referred to as Collision. Both components would be deposited in the same bucket if they collide.

6)Searching element in Hash Table

If we want to check for an element in a hash table, we must first calculate the hash code for that element. Then, using the hash code, we’ll go straight to the bucket where this element will be saved. Now, there may be several elements in that bucket; in that case, we’ll look for our element only in those elements.

Assume we want to find 45 in the hash table in the previous case.

Then we’ll measure the hash code, which will be 45 %10 = 5.

Then we’ll go straight to bucket no. 5 and search for the factor there.

7)Best Case Time Complexity in Hashing

From the viewpoint of searching, each bucket containing only one element in the Hash table is the best case scenario. In such a case, searching time would require the following effort:

The complexity of calculating hash codes is O(1)

If the bucket only contains one element, choosing that element from the bucket is a complex task O(1)

Consequently In the best case scenario, finding an element would be difficult O(1)

While the complexity of a collision, i.e. several elements in a bucket, will be O(no. of elements in bucket), it will be much less than the complexity of searching an element from a List, which will be O(n).

8)Worst Case Time Complexity in Hashing

The worst case scenario in hashing is when the Hash Function is not correctly implemented, resulting in the same hash code for several elements.

Assume the following is our hash function:

Hash code = given_element / 100

Then, in the preceding case, the hash code for all elements ( 32 , 45 , 64 , 92 , 57 , 88 , 73 ) will be 0. As a result, all elements will be saved in bucket 0.

Since all components are in the same bucket, complexity is at its highest in this case. As a result, the complexity will be O(n) since it must check for the appropriate element among all elements.

Related Programs:

Designing Code for Fibonacci Sequence without Recursion

Designing Code for Fibonacci Sequence without Recursion

Fibonacci sequence:

The Fibonacci sequence is a sequence type in which the sum of the previous two numbers is each consecutive number.

First few Fibonacci numbers are 0 1 1 2 3 5 8 …..etc.

Fibonacci sequence without recursion:

Let us now write code to display this sequence without recursion. Because recursion is simple, i.e. just use the concept,

Fib(i) = Fib(i-1) + Fib(i-2)

However, because of the repeated calculations in recursion, large numbers take a long time.

So, without recursion, let’s do it.

Approach:

  • Create two variables to hold the values of the previous and second previous numbers.
  • Set both variables to 0 to begin.
  • Begin a loop until N is reached, and for each i-th index
    • Print the sum of the previous and second most previous i.e sum= previous + second previous
    • Assign previous to second previous i.e. second previous= previous
    • Assign sum to previous i.e previous=sum

Below is the implementation:

1)C++ implementation of above approach.

#include <iostream>
using namespace std;
int main()
{ // given number
    int n = 10;
    // initializing previous and second previous to 0
    int previous = 0;
    int secondprevious = 0;
    int i;
    // loop till n
    for (i = 0; i < n; i++) {
        // initializing sum to previous + second previous
        int sum = previous + secondprevious;
        cout << sum << " ";
        if (!sum)
            previous = 1;
        secondprevious = previous;
        previous = sum;
    }
    return 0;
}

2)Python implementation of above approach.

# given number
n = 10
# initializing previous and second previous to 0
previous = 0
secondprevious = 0
# loop till n
for i in range(n):
    sum = previous + secondprevious
    print(sum, end=" ")
    if (not sum):
        previous = 1
    # initializing value to previous to second previous
    secondprevious = previous
    previous = sum

Output:

0 1 1 2 3 5 8 13 21 34

Related Programs:

Python Program to Print Mirrored Right Triangle Star Pattern

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

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

Examples:

Example1:

Input:

given number of rows of the right triangle =6

Output:

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

Example2:

Input:

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

Output:

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

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

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

Method #1: Using For loop (Star Character)

Approach:

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

1) Python Implementation:

Below is the implementation:

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

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

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

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the right triangle pattern
    //  as static input and store it in a variable.
    int triNumRows = 6;
    // Iterate from 1 to given rows using First for loop.
    for (int m = 1; m <= triNumRows; m++) {
        // Iterate from 1 to given rows using another for
        // loop(Nested For loop)
        for (int n = 1; n <= triNumRows; n++) {
            // Check if the iterator value of the inner
            // for loop is less
            //       than
            //  or equal to given rows
            // first iterator value using If statement.
            if (n <= triNumRows - m) {
                // If the statement is true then print
                // space.
                printf("  ");
            }
            else {
                // Else print star character with space.
                printf("* ");
            }
        }
        // Print the newline character after the exit of the
        // inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For Loop(User Character)

Approach:

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

1) Python Implementation:

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

Below is the implementation:

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

Output:

Enter some random number of rows of the Right Triangle Pattern = 6
Enter some random character = $
               $ 
            $ $ 
         $ $ $ 
      $ $ $ $ 
   $ $ $ $ $ 
$ $ $ $ $ $

2) C++ Implementation

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

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

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

    return 0;
}

Output:

Enter some random number of rows of the Right Triangle Pattern = 
6
Enter some random character = 
$
               $ 
            $ $ 
         $ $ $ 
      $ $ $ $ 
   $ $ $ $ $ 
$ $ $ $ $ $

3) C Implementation

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

Below is the implementation:

#include <stdio.h>

int main()
{

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

Output:

6$
               $ 
            $ $ 
         $ $ $ 
      $ $ $ $ 
   $ $ $ $ $ 
$ $ $ $ $ $

Related Programs: