CPP Programming

Linked List Data Structure

Linked List Data Structure:

A linked list is a type of data structure that consists of a chain of nodes, each of which contains a value and a pointer to the next node in the chain.

The list’s head pointer points to the first node, and the list’s last element points to null. The head pointer points to null when the list is empty.

Linked lists can grow in size dynamically, and inserting and deleting elements from them is simple because, unlike arrays, we only need to change the pointers to the previous and next elements to insert or delete an element.

Linked lists are commonly used in the construction of file systems, adjacency lists, and hash tables.

Why Linked List?

Arrays can be used to store similar types of linear data, but they have the following limitations.
1) Because the array size is fixed, we must know the maximum number of elements in advance. Furthermore, regardless of usage, the allocated memory is always equal to the upper limit.
2) Inserting a new element in an array of elements is expensive because room must be made for the new elements, and existing elements must be shifted to make room, whereas in a linked list if we have the head node, we can traverse to any node through it and insert a new node at the required position.

Advantages of Linked Lists over arrays

1)Dynamic Data Structure:

  • Because the linked list is a dynamic data structure that can shrink and grow at runtime by deallocating or allocating memory, there is no need for initial size.
  • In contrast, in an array, an initial size must be declared, and the number of elements cannot exceed that size.

2) No Memory Wastage:

  • There is no memory waste because the size of a linked list can grow or shrink at runtime. Only the necessary memory is allocated.
  • In arrays, we must first initialize it with a size that we may or may not fully utilize. Thus, memory waste may occur.

3)Implementation:

A Linked List can be used to easily implement some very useful data structures such as queues and stacks.

4)Insertion and Deletion Operation:

Insertion and deletion operations in a Linked List are simple because there is no need to shift every element after insertion or deletion. Only the address in the pointers needs to be changed.

Disadvantages of Linked Lists over arrays

1)Memory Usage:

A linked list requires more memory than an array because it includes a pointer field in addition to the data field. The pointer field, like all other fields, requires memory to store the address of the next node.

2)Random Access:

To get to node an at index x in a linked list, we must first go through all the nodes before it. In the case of an array, however, we can use arr[x] to directly access an element at index x.

3)Reverse Traversal:

  • Reverse traversal is not possible in a singly linked list because each node stores only the address of the next node. Reverse traversal is possible in the case of a doubly-linked list, but it consumes more memory because we must allocate extra memory to store the previous pointer.
  • With the help of a for loop, we can perform a simple reverse traversal in arrays.

Representation:

A pointer to the first node of a linked list is used to represent it. The first node is referred to as the head. When the linked list is empty, the value of the head is NULL.
A list node is made up of at least two parts:
1) data (we can store integers, strings, or any type of data).
2) A pointer to the next node (or a reference to it) (connects one node to another)

Example Node of a Linked List

Program for Multiplication of Two Matrices in Python and C++ Programming

Python Program for Multiplication of Two Matrices | How do you do Matrix Multiplication in Python?

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.

Looking across the web for guidance on Multiplication of Two Matrices in Python? Before you begin your learning on Matrix Multiplication of Two Matrices know what is meant by a Matrix and what does Matrix Multiplication in actual means. This tutorial is going to be extremely helpful for you as it assists you completely on How to Multiply Two Matrices in Python using different methods like nested loops, nested list comprehension, etc. Furthermore, we have written simple python programs for multiplying two matrices clearly.

What is a Matrix?

A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.

Example:

Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.

In this order, the dimensions of a matrix indicate the number of rows and columns.

Here as there are 5 rows and 4 columns it is called a 5*4 matrix.

What is Matrix Multiplication?

Matrix multiplication is only possible if the second matrix’s column equals the first matrix’s rows. A matrix can be represented in Python as a nested list ( a list inside a list ).

Examples for Matrix Multiplication:

Example 1:

Input:

Matrix 1  =  [  1  -8    7  ]
                    [  0   2    5  ]
                    [  1   6    2 ]
                      
Matrix 2 =   [   5    2   -6  ]
                    [   1    8    9 ]
                    [   3    5    7 ]

Output:

printing the matrix A :
1 -8 7
0 2 5
1 6 2
printing the matrix B :
5 2 -6
1 8 9
3 5 7
Printing the product of matrices : 
18 -27 -29
17 41 53
17 60 62

Example 2:

Input:

Matrix 1  =  1 2 3 
                  -5 8 7 
                   6 4 5 
                      
Matrix 2 =  2 7 9 
                  0 -8 5 
                  6 -5 2

Output:

Enter the number of rows and columns of the first matrix3
3
Enter the number of rows and columns of the second matrix3
3
Enter element A11 = 1
Enter element A12 = 2
Enter element A13 = 3
Enter element A21 = -5
Enter element A22 = 8
Enter element A23 = 7
Enter element A31 = 6
Enter element A32 = 4
Enter element A33 = 5

Enter elements of 2nd matrix: 
Enter element B11 = 2
Enter element B12 = 7
Enter element B13 = 9
Enter element B21 = 0
Enter element B22 = -8
Enter element B23 = 5
Enter element B31 = 6
Enter element B32 = -5
Enter element B33 = 2

printing the matrix A
1 2 3 
-5 8 7 
6 4 5

printing the matrix B
2 7 9 
0 -8 5 
6 -5 2 
Print the product of two matrices A and B
20 -24 25 
32 -134 9 
42 -15 84

Given two matrices, the task is to write a program in Python and C++ to multiply the two matrices.

Simple Python Program for Matrix Multiplication

Method #1: Using Nested Loops in Python

A matrix can be implemented as a nested list in Python (list inside a list).

Each element can be thought of as a row in the matrix.

X[0] can be used to choose the first row. Furthermore, the element in the first row, the first column can be chosen as X[0][0].

The multiplication of two matrices X and Y is defined if the number of columns in X equals the number of rows in Y.

Here we give the matrix input as static.

XY is defined and has the dimension n x l if X is a n x m matrix and Y is a m x l matrix (but YX is not defined). In Python, there are a few different approaches to implement matrix multiplication.

Below is the  implementation:

# given matrix A
A = [[1, - 8, 7],
     [0, 2, 5],
     [1, 6, 2]]
# given matrix B
B = [[5, 2, - 6],
     [1, 8, 9],
     [3, 5, 7]]
# Initialize the product of matrices elements to 0
matrixProduct = [[0, 0, 0],
                 [0, 0, 0],
                 [0, 0, 0]]
# Traverse the rows of A
for i in range(len(A)):
    #  Traverse the  columns of B
    for j in range(len(B[0])):
        # Traverse the  rows of B
        for k in range(len(B)):
            matrixProduct[i][j] += A[i][k] * B[k][j]
# printing the matrix A
print("printing the matrix A :")
for rows in A:
    print(*rows)
# printing the matrix B
print("printing the matrix B :")
for rows in B:
    print(*rows)
# printing the product of matrices
print("Printing the product of matrices : ")
for rows in matrixProduct:
    print(*rows)

Python Program for Matrix Multiplication using Nested Loops

Output:

printing the matrix A :
1 -8 7
0 2 5
1 6 2
printing the matrix B :
5 2 -6
1 8 9
3 5 7
Printing the product of matrices : 
18 -27 -29
17 41 53
17 60 62

Explanation:

To go through each row and column in our program, we used nested for loops. In the end, we add up the sum of the items.

This technique is simple, but it becomes computationally expensive as the order of the matrix increases.

We recommend NumPy for larger matrix operations because it is several (in the order of 1000) times faster than the above code.

Method #2: Matrix Multiplication List Comprehension Python

This program produces the same results as the previous one. To understand the preceding code, we must first know the built-in method zip() and how to unpack an argument list with the * operator.

To loop through each element in the matrix, we utilized nested list comprehension. At first glance, the code appears to be complicated and difficult to understand. However, once you’ve learned list comprehensions, you won’t want to go back to nested loops.

Below is the implementation:

# given matrix A
A = [[1, - 8, 7],
     [0, 2, 5],
     [1, 6, 2]]
# given matrix B
B = [[5, 2, - 6],
     [1, 8, 9],
     [3, 5, 7]]
# using list comprehension to do product of matrices
matrixProduct = [[sum(a*b for a, b in zip(row, col))
                  for col in zip(*B)] for row in A]

# printing the matrix A
print("printing the matrix A :")
for rows in A:
    print(*rows)
# printing the matrix B
print("printing the matrix B :")
for rows in B:
    print(*rows)
# printing the product of matrices
print("Printing the product of matrices : ")
for rows in matrixProduct:
    print(*rows)

Python Program for Matrix Multiplication using List Comprehension

Output:

printing the matrix A :
1 -8 7
0 2 5
1 6 2
printing the matrix B :
5 2 -6
1 8 9
3 5 7
Printing the product of matrices : 
18 -27 -29
17 41 53
17 60 62

Method #3: Using Nested Loops in C++

We used nesting loops in this program to iterate through and row and column. In order to multiply two matrices, we will do the multiplication of matrices as below

Let us take dynamic input in this case.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    int A[100][100], B[100][100], product[100][100], row1,
        col1, row2, col2, i, j, k;

    cout << "Enter the number of rows and columns of the "
            "first matrix";
    cin >> row1 >> col1;
    cout << "Enter the number of rows and columns of the "
            "second matrix";
    cin >> row2 >> col2;

    // Initializing matrix A with the user defined values
    for (i = 0; i < row1; ++i)
        for (j = 0; j < col1; ++j) {
            cout << "Enter element A" << i + 1 << j + 1
                 << " = ";
            cin >> A[i][j];
        }
    // Initializing matrix B with the user defined values
    cout << endl
         << "Enter elements of 2nd matrix: " << endl;
    for (i = 0; i < row2; ++i)
        for (j = 0; j < col2; ++j) {
            cout << "Enter element B" << i + 1 << j + 1
                 << " = ";
            cin >> B[i][j];
        }

    // Initializing the resultant product matrix with 0 to
    // all elements
    for (i = 0; i < row1; ++i)
        for (j = 0; j < col2; ++j) {
            product[i][j] = 0;
        }

    // Calculating the product of two matrices A and B
    for (i = 0; i < row1; ++i)
        for (j = 0; j < col2; ++j)
            for (k = 0; k < col1; ++k) {
                product[i][j] += A[i][k] * B[k][j];
            }
    // printing matrix A
    cout << endl << " printing the matrix A" << endl;
    for (i = 0; i < row1; ++i) {
        for (j = 0; j < col1; ++j) {
            cout << A[i][j] << "  ";
        }
        cout << endl;
    }
    // printing matrix B
    cout << endl << " printing the matrix B" << endl;
    for (i = 0; i < row2; ++i) {
        for (j = 0; j < col2; ++j) {
            cout << B[i][j] << "  ";
        }
        cout << endl;
    }

    // Print the product of two matrices A and B
    cout << "Print the product of two matrices A and B"
         << endl;
    for (i = 0; i < row1; ++i) {
        for (j = 0; j < col2; ++j) {
            cout << product[i][j] << "  ";
        }
        cout << endl;
    }

    return 0;
}

Output:

Enter the number of rows and columns of the first matrix3
3
Enter the number of rows and columns of the second matrix3
3
Enter element A11 = 1
Enter element A12 = 2
Enter element A13 = 3
Enter element A21 = -5
Enter element A22 = 8
Enter element A23 = 7
Enter element A31 = 6
Enter element A32 = 4
Enter element A33 = 5

Enter elements of 2nd matrix: 
Enter element B11 = 2
Enter element B12 = 7
Enter element B13 = 9
Enter element B21 = 0
Enter element B22 = -8
Enter element B23 = 5
Enter element B31 = 6
Enter element B32 = -5
Enter element B33 = 2

printing the matrix A
1 2 3 
-5 8 7 
6 4 5

printing the matrix B
2 7 9 
0 -8 5 
6 -5 2 
Print the product of two matrices A and B
20 -24 25 
32 -134 9 
42 -15 84

How to Multiply Two Matrices in Python using Numpy?

import numpy as np

C = np.array([[3, 6, 7], [5, -3, 0]])
D = np.array([[1, 1], [2, 1], [3, -3]])
E = C.dot(D)
print(E)

''' 

Output:

[[ 36 -12]
[ -1 2]]

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.

Related Programs:

Write a Program for Matrix Subtraction

Program to Subtract two Matrices in Python & C++ Programming

What is a matrix:

A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.

Example:

Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.

In this order, the dimensions of a matrix indicate the number of rows and columns.

Here as there are 5 rows and 4 columns it is called as 5*4 matrix.

What is matrix subtraction:

Given two matrices of same order(dimensions) then we can easily add the two matrices by doing the difference of corresponding elements in both the matrices.

Example:

 

Here Matrix C is matrix subtraction  of the matrices A and B.

Examples for matrix addition:

Input:

Matrix 1  =    2    3   1
                      1    2   3
                     -3   0   2    
Matrix 2 =      1    4    2
                     -4    5   -1
                      2    1     4

Output:

Printing the difference of matrices : 
1 -1 -1
5 -3 4
-5 -1 -2

Explanation:

 Here we subtracted the corresponding elements in both matrices

Program for Matrix Subtraction

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

Method #1:Using nested Loops in Python

A matrix can be implemented as a nested list in Python (list inside a list). Each variable can be thought of as a row in the matrix.

Approach:

  • Create two two-dimensional arrays, a and b, and set their values. (In this case, we used a  initialised statically)
  • Calculate the number of rows and columns in the array a (since the dimensions of both arrays are the same) and store it in the variables rows and columns.
    Declare a new array diff, this time with dimensions in rows and columns.
  • Calculate the difference between the respective elements by looping around the arrays a and b.
  • Print the difference of two matrices.

Below is the  implementation:

# given matrix A
A = [[2, 3, 1],
     [1, 2, 3],
     [-3, 0, 2]]
# given matrix B
B = [[1, 4, 2],
     [-4, 5, -1],
     [2, 1, 4]]
# Initialize the difference of matrices elements to 0
matrixDiff = [[0, 0, 0],
              [0, 0, 0],
              [0, 0, 0]]
# Traverse the rows
for rows in range(len(A)):
    # Traverse the  columns
    for columns in range(len(A[0])):
        matrixDiff[rows][columns] = A[rows][columns] - B[rows][columns]
# printing the difference of matrices
print("Printing the difference of matrices : ")
for rows in matrixDiff:
    print(*rows)

Output:

Printing the difference of matrices : 
1 -1 -1
5 -3 4
-5 -1 -2

Method #2:Using List Comprehension in Python

The program’s output is the same as previously. For iterating through each element in the array, we used the nested list comprehension.

Understanding the list helps us to write concise codes and we need to always try to use them in Python. They’re really beneficial.

Below is the implementation:

# given matrix A
A = [[2, 3, 1],
     [1, 2, 3],
     [-3, 0, 2]]
# given matrix B
B = [[1, 4, 2],
     [-4, 5, -1],
     [2, 1, 4]]
# Initialize the difference of matrices elements to 0
matrixDiff = [[0, 0, 0],
              [0, 0, 0],
              [0, 0, 0]]

# using list comprehension
matrixDiff = [[A[i][j] - B[i][j]
               for j in range(len(A[0]))] for i in range(len(A))]

# printing the difference of matrices
print("Printing the difference of matrices : ")
for rows in matrixDiff:
    print(*rows)

Output:

Printing the sum of matrices : 
16 -1 -5
6 11 6

Method #3:Using nested loops in C++

We used nesting loops in this program to iterate through and row and column. In the two matrices we will subract the appropriate elements and store them in the result at each level.

Let us take dynamic input in this case.

Below is the implementation:

#include <iostream>
using namespace std;
int main()
{
    int rows, columns, A[100][100], B[100][100], DiffMatrix[100][100], i, j;
    cout << "Enter the number of rows of the matrix "<<endl;
    cin >> rows;
    cout << "Enter the number of columns of the matrix"<<endl;
    cin >> columns;
    cout << "Enter the elements of first matrix " << endl;
    // Initializing matrix A with the user defined values
    for(i = 0; i < rows; ++i)
       for(j = 0; j < columns; ++j)
       {
           cout << "Enter element A" << i + 1 << j + 1 << " = ";
           cin >> A[i][j];
       }
   // Initializing matrix B with the user defined values
    cout << endl << "Enter elements of 2nd matrix: " << endl;
    for(i = 0; i < rows; ++i)
       for(j = 0; j < columns; ++j)
       {
           cout << "Enter element B" << i + 1 << j + 1 << " = ";
           cin >> B[i][j];
       }
    // Performing matrix addition by doing diffeerence of given two matrices A and B
    for(i = 0; i < rows; ++i)
        for(j = 0; j < columns; ++j)
            DiffMatrix[i][j] = A[i][j] - B[i][j];
    //printing matrix A
    cout << endl << " printing the matrix A" << endl;
    for (i = 0; i < rows; ++i) {
    for (j = 0; j < columns; ++j) {
        cout << A[i][j] << "  ";
    }
    cout << endl;
}
    //printing matrix B
    cout << endl << " printing the matrix B" << endl;
    for (i = 0; i < rows; ++i) {
    for (j = 0; j < columns; ++j) {
        cout << B[i][j] << "  ";
    }
    cout << endl;
}
    //printing the difference of matrices
    cout << endl << " printing the difference of matrices A and B" << endl;
    
    for (i = 0; i < rows; ++i) {
    for (j = 0; j < columns; ++j) {
        cout << DiffMatrix[i][j] << "  ";
    }
    cout << endl;
}
    return 0;
}

Output:

Enter the number of rows of the matrix 
4
Enter the number of columns of the matrix
4
Enter the elements of first matrix 
Enter element A11 = 1
Enter element A12 = 2
Enter element A13 = 3
Enter element A14 = 4
Enter element A21 = 5
Enter element A22 = 6
Enter element A23 = 5
Enter element A24 = 2
Enter element A31 = -5
Enter element A32 = 9
Enter element A33 = 1
Enter element A34 = 5
Enter element A41 = 0
Enter element A42 = 5
Enter element A43 = 2
Enter element A44 = 5

Enter elements of 2nd matrix: 
Enter element B11 = 1
Enter element B12 = 9
Enter element B13 = 5
Enter element B14 = 3
Enter element B21 = 8
Enter element B22 = -4
Enter element B23 = -8
Enter element B24 = 0
Enter element B31 = 5
Enter element B32 = 1
Enter element B33 = 7
Enter element B34 = -3
Enter element B41 = -8
Enter element B42 = 1
Enter element B43 = 2
Enter element B44 = 0

printing the matrix A
1 2 3 4 
5 6 5 2 
-5 9 1 54 
0 5 2 5

printing the matrix B
1 9 5 3 
8 -4 -38 0 
5 1 7 -3 
-8 1 2 0

printing the difference of matrices A and B
0 -7 -2 1 
-3 10 43 2 
-10 8 -6 57 
8 4 0 5

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 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 Pattern 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 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:

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 Rhombus Star Pattern

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

Given the number of rows of the rhombus, the task is to print the Rhombus star pattern in C, C++, and Python.

Examples:

Example1:

Input:

given number of rows of rhombus =19

Output:

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

Example2:

Input:

given number of rows of rhombus =9
given character to print =-

Output:

        ---------
       ---------
      ---------
     ---------
    ---------
   ---------
  ---------
 ---------
---------

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

Below are the ways to print the Rhombus Star Pattern 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.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the rhombus as static input and store it in a variable.
rhombusrows = 19
# 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):
        print('*', end='')
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#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++) {
            cout << "* ";
        }
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#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++) {
            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.
    • 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:

# 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 = ')
for m in range(rhombusrows, 0, -1):
    for n in range(1, m):
        print(' ', end='')
    for k in range(0, rhombusrows):
        print(characte, end='')
    print()

Output:

Enter some random number of rows of the rhombus= 3
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:

#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;
    // 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++) {
            cout << characte << " ";
        }
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the rhombus = 
19
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:

#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++) {
            printf("%c ",characte);
        }
        printf("\n");
    }
}

Output:

9-
        ---------
       ---------
      ---------
     ---------
    ---------
   ---------
  ---------
 ---------
---------

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: