Python Program to Print Series 1, -2, 6, -15, 31 … N

Program to Print Series 1, -2, 6, -15, 31 ... N

In the previous article, we have discussed Python Program to Print Series 1, 3, 7, 15, 31 … N
Given a number N and the task is to print the series (1, -2, 6, -15, 31 … N) till the given number N in Python.

Examples:

Example1:

Input:

Given Number (Limit) = 8

Output:

The above series till the given number{ 8 } is :
1 -2 6 -15 31 -56 92 -141

Example2:

Input:

Given Number (Limit) = 11

Output:

The above series till the given number{ 11 } is :
1 -2 6 -15 31 -56 92 -141 205 -286 386

Program to Print Series 1, -2, 6, -15, 31 … N in Python

Below are the ways to print the series (1, -2, 6, -15, 31 … N) till the given number N in Python :

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number N (Limit) as static input and store it in a variable.
  • Take a variable to say itr and initialize its value to 1.
  • Take another variable say previous_val and initialize its value to 1.
  • Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
  • Inside the loop, check if the above itr value is even or not using the if conditional statement.
  • If it is true, then print the value of above previous_val multiplied with -1 separated by spaces.
  • Else, Print the value of the above previous_val separated by spaces.
  • Calculate the value of itr raised to power 2 using the pow() function and add it to the previous_val.
  • Store it in the same variable previous_val.
  • Increment the above itr value by 1.
  • The Exit of the Program.

Below is the implementation:

# Give the number N (Limit) as static input and store it in a variable.
gvn_numb = 8
# Take a variable to say itr and initialize its value to 1.
itr = 1
# Take another variable say previous_val and initialize its value to 1.
previous_val = 1
print("The above series till the given number{", gvn_numb, "} is :")
# Loop until the above-declared variable itr value is less than or equal to the
# given number using the while loop.
while itr <= gvn_numb:
   # Inside the loop, check if the above itr value is even or not using the if
   # conditional statement.
    if(itr % 2 == 0):
         # If it is true, then print the value of above previous_val multiplied with -1
         # separated by spaces.
        print(-1*previous_val, end=" ")
    else:
        # Else, Print the value of the above previous_val separated by spaces.
        print(previous_val, end=" ")
   # Calculate the value of itr raised to power 2 using the pow() function and
   # add it to the previous_val.
   # Store it in the same variable previous_val.
    previous_val += pow(itr, 2)
    # Increment the above itr value by 1.
    itr += 1

Output:

The above series till the given number{ 8 } is :
1 -2 6 -15 31 -56 92 -141

Method #2: Using While loop (User Input)

Approach:

  • Give the number N (Limit) as user input using the int(input()) function and store it in a variable.
  • Take a variable to say itr and initialize its value to 1.
  • Take another variable say previous_val and initialize its value to 1.
  • Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
  • Inside the loop, check if the above itr value is even or not using the if conditional statement.
  • If it is true, then print the value of above previous_val multiplied with -1 separated by spaces.
  • Else, Print the value of the above previous_val separated by spaces.
  • Calculate the value of itr raised to power 2 using the pow() function and add it to the previous_val.
  • Store it in the same variable previous_val.
  • Increment the above itr value by 1.
  • The Exit of the Program.

Below is the implementation:

# Give the number N (Limit) as user input using the int(input()) function and 
# store it in a variable.
gvn_numb = int(input("Enter some Random Number = "))
# Take a variable to say itr and initialize its value to 1.
itr = 1
# Take another variable say previous_val and initialize its value to 1.
previous_val = 1
print("The above series till the given number{", gvn_numb, "} is :")
# Loop until the above-declared variable itr value is less than or equal to the
# given number using the while loop.
while itr <= gvn_numb:
   # Inside the loop, check if the above itr value is even or not using the if
   # conditional statement.
    if(itr % 2 == 0):
         # If it is true, then print the value of above previous_val multiplied with -1
         # separated by spaces.
        print(-1*previous_val, end=" ")
    else:
        # Else, Print the value of the above previous_val separated by spaces.
        print(previous_val, end=" ")
   # Calculate the value of itr raised to power 2 using the pow() function and
   # add it to the previous_val.
   # Store it in the same variable previous_val.
    previous_val += pow(itr, 2)
    # Increment the above itr value by 1.
    itr += 1

Output:

Enter some Random Number = 11
The above series till the given number{ 11 } is :
1 -2 6 -15 31 -56 92 -141 205 -286 386

Dive into numerous Python Programming Language Examples for practice and get the best out of the tutorial and learn python one step at a time.

Python Program to Print Series 2, 4, 7, 12, 21, … N

Program to Print Series 2, 4, 7, 2, 21, ... N

In the previous article, we have discussed Python Program to Print Series 2 ,15, 41, 80…n
Given a number N and the task is to print the series (2, 4, 7, 12, 21, … N) till the given number N in Python.

Examples:

Example1:

Input:

Given Number (Limit) = 8

Output:

The above series till the given number{ 8 } is :
2 4 7 12 21 38 71 136

Example2:

Input:

Given Number (Limit) = 10

Output:

The above series till the given number{ 10 } is :
2 4 7 12 21 38 71 136 265 522

Program to Print Series 2 4 7 12 21 … N in Python

Below are the ways to print the series (2, 4, 7, 12, 21, … N) till the given number N in Python:

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number N (Limit) as static input and store it in a variable.
  • Take a variable to say itr and initialize its value to 0.
  • Take another variable say previous_val and initialize its value to 2.
  • Print the value 2 separated by spaces.
  • Loop until the above-declared variable itr value is less than the given number-1 using the while loop.
  • Inside the loop, Multiply the variable previous_val with 2 and subtract the above itr value from it.
  • Store it in the same variable previous_val.
  • Print the value of the above previous_val separated by spaces.
  • Increment the above itr value by 1.
  • The Exit of the Program.

Below is the implementation:

# Give the number N(limit) as static input and store it in a variable.
gvn_numb = 8
# Take a variable to say itr and initialize its value to 0.
itr = 0
# Take another variable say previous_val and initialize its value to 2.
previous_val = 2
print("The above series till the given number{", gvn_numb, "} is :")
# print the value 2 separated by spaces.
print("2 ",end="")
# Loop until the above-declared variable itr value is less than the
# given number-1 using the while loop.
while itr<gvn_numb-1:
    #Inside the loop, Multiply the variable previous_val with 2 and subtract the above
    #itr value from it.
    #Store it in the same variable previous_val.
    previous_val = (previous_val * 2) -itr
    # Print the value of the above previous_val separated by spaces.
    print(previous_val,end=" ")
    # Increment the above itr value by 1.
    itr+=1

Output:

The above series till the given number{ 8 } is :
2 4 7 12 21 38 71 136

Method #2: Using While loop (User Input)

Approach:

  • Give the number N (Limit) as user input using the int(input()) function and store it in a variable.
  • Take a variable to say itr and initialize its value to 0.
  • Take another variable say previous_val and initialize its value to 2.
  • Print the value 2 separated by spaces.
  • Loop until the above-declared variable itr value is less than the given number-1 using the while loop.
  • Inside the loop, Multiply the variable previous_val with 2 and subtract the above itr value from it.
  • Store it in the same variable previous_val.
  • Print the value of the above previous_val separated by spaces.
  • Increment the above itr value by 1.
  • The Exit of the Program.

Below is the implementation:

# Give the number N (Limit) as user input using the int(input()) function and 
# store it in a variable.
gvn_numb = int(input("Enter some Random Number = "))
# Take a variable to say itr and initialize its value to 0.
itr = 0
# Take another variable say previous_val and initialize its value to 2.
previous_val = 2
print("The above series till the given number{", gvn_numb, "} is :")
# print the value 2 separated by spaces.
print("2 ",end="")
# Loop until the above-declared variable itr value is less than the
# given number-1 using the while loop.
while itr<gvn_numb-1:
    #Inside the loop, Multiply the variable previous_val with 2 and subtract the above
    #itr value from it.
    #Store it in the same variable previous_val.
    previous_val = (previous_val * 2) -itr
    # Print the value of the above previous_val separated by spaces.
    print(previous_val,end=" ")
    # Increment the above itr value by 1.
    itr+=1

Output:

Enter some Random Number = 10
The above series till the given number{ 10 } is :
2 4 7 12 21 38 71 136 265 522

Remediate your knowledge gap by attempting the Python Code Examples regularly and understand the areas of need and work on them.

Python Program to Print Series 2 ,15, 41, 80…n

Program to Print Series 2 ,15, 41, 80...n

In the previous article, we have discussed Python Program to Print Series 1 9 17 33 49 73 97 …N
Given a number N (Limit) and the task is to print the series (2,15,41,80…N) till the given number N in Python.

Examples:

Example1:

Input:

Given Number(limit) = 15

Output:

The above series till the given number{ 15 } is :
2 15 41 80 132 197 275 366 470 587 717 860 1016 1185 1367

Example2:

Input:

Given Number(limit) = 20

Output:

The above series till the given number{ 20 } is :
2 15 41 80 132 197 275 366 470 587 717 860 1016 1185 1367 1562 1770 1991 2225 2472

Program to Print Series 2,15,41,80…n in Python

Below are the ways to print the series ( 2,15,41,80…N) till the given number N in Python:

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number N (limit) as static input and store it in a variable.
  • Take a variable to say itr and initialize its value to 1.
  • Take another variable say valu and initialize its value to 2.
  • Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
  • Inside the loop, print the value of the above variable valu separated by spaces.
  • Multiply the value of itr with 13 and add the result to the valu.
  • Store the above result in the same variable valu.
  • Increment the above itr value by 1.
  • The Exit of the Program.

Below is the implementation:

# Give the number N (limit)as static input and store it in a variable.
gvn_numb = 15
# Take a variable to say itr and initialize its value to 1.
itr = 1
# Take another variable say valu and initialize its value to 2.
valu=2
print("The above series till the given number{", gvn_numb, "} is :")
# Loop until the above-declared variable itr value is less than or equal to the
# given number using the while loop.
while(itr<=gvn_numb):
    #Inside the loop, print the value of the above variable valu 
    #separated by spaces.
    print(valu,end=" ")
    #Multiply the value of itr with 13 and add the result to the valu.
    #Store the above result in the same variable valu.
    valu+=itr*13
    #Increment the above itr value by 1.
    itr+=1

Output:

The above series till the given number{ 15 } is :
2 15 41 80 132 197 275 366 470 587 717 860 1016 1185 1367

Method #2: Using While loop (User Input)

Approach:

  • Give the number N (limit) as user input using the int(input()) function and store it in a variable.
  • Take a variable to say itr and initialize its value to 1.
  • Take another variable say valu and initialize its value to 2.
  • Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
  • Inside the loop, print the value of the above variable valu separated by spaces.
  • Multiply the value of itr with 13 and add the result to the valu.
  • Store the above result in the same variable valu.
  • Increment the above itr value by 1.
  • The Exit of the Program.

Below is the implementation:

# Give the number N (limit) as user input using the int(input()) function and 
# store it in a variable.
gvn_numb = int(input("Enter some Random Number = "))
# Take a variable to say itr and initialize its value to 1.
itr = 1
# Take another variable say valu and initialize its value to 2.
valu=2
print("The above series till the given number{", gvn_numb, "} is :")
# Loop until the above-declared variable itr value is less than or equal to the
# given number using the while loop.
while(itr<=gvn_numb):
    #Inside the loop, print the value of the above variable valu 
    #separated by spaces.
    print(valu,end=" ")
    #Multiply the value of itr with 13 and add the result to the valu.
    #Store the above result in the same variable valu.
    valu+=itr*13
    #Increment the above itr value by 1.
    itr+=1

Output:

Enter some Random Number = 20
The above series till the given number{ 20 } is :
2 15 41 80 132 197 275 366 470 587 717 860 1016 1185 1367 1562 1770 1991 2225 2472

Explore more Example Python Programs with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.

Python Pandas Period.day Attribute

Python Pandas Period.day Attribute

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

Pandas

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

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

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

Period in Pandas:

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

Period.day Pandas:

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

Syntax :

Period.day

Parameters:

It has no arguments/parameters.

Return :

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

Pandas Period.day Attribute in Python

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

Example1

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

Approach:

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

Below is the implementation:

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

Output:

The given period object:
2020-09-28

The day value of the given period object:
28

Explanation:

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

Example2

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

Approach:

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

Below is the implementation:

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

Output:

The given period object:
2021Q2

The day value of the given quarter:
30

Explanation:

Here it prints the last day of the second quarter.

Python Program to Find the Product of two Numbers Using Recursion

Program to Find the Product of two Numbers Using Recursion

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

Recursion in Python:

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

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

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

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

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

Examples:

Example1:

Input:

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

Output:

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

Example2:

Input:

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

Output:

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

Program to Find the Product of two Numbers Using Recursion

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

1)Using Recursion(Static Input)

Approach:

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

Below is the implementation:

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


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

Output:

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

2)Using Recursion(User Input separated by spaces)

Approach:

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

Below is the implementation:

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


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

Output:

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

Explanation:

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

Related Programs:

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

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

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

Examples:

Example1:

Input:

Given number of rows = 8

Output:

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

Example2:

Input:

Given number of rows = 13

Output:

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

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

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

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Inside the For, loop Take a variable(say Tempo) and initialize its value with 1.
  • Print the value of the Tempo variable with space.
  • Loop from the number of rows -m-1 to 0 in decreasing order using another for loop(Nested For loop) where m is the iterator value of the parent For loop.
  • Print the Star Character with space.
  • Increment the value of Tempo by 1.
  • Print the value of the Tempo variable with space.
  • Print the Newline character after the end of the inner loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

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

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

Output:

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

2) C++ Implementation

Below is the implementation:

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

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

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

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

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

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

Output:

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

Method #2: Using For Loop (User Input)

Approach:

  • Give the number of rows as user input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Inside the For, loop Take a variable(say Tempo) and initialize its value with 1.
  • Print the value of the Tempo variable with space.
  • Loop from the number of rows -m-1 to 0 in decreasing order using another for loop(Nested For loop) where m is the iterator value of the parent For loop.
  • Print the Star Character with space.
  • Increment the value of Tempo by 1.
  • Print the value of the Tempo variable with space.
  • Print the Newline character after the end of the inner loop.
  • The Exit of the Program.

1) Python Implementation

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

Below is the implementation:

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

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

Output:

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

2) C++ Implementation

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

Below is the implementation:

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

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

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

    return 0;
}

Output:

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

3) C Implementation

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

Below is the implementation:

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

    // Give the number of rows as user input using scanf and
    // store it in a variable.
    int numberOfRows;
    scanf("%d", &numberOfRows);
   // Loop from 0 to the number of rows using For loop.

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

Output:

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

Related Programs:

Python Program to Print 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 the Pyramid of Horizontal Number Tables

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

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

Examples:

Example1:

Input:

Given number of rows =9

Output:

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

Example2:

Input:

Given number of rows =13

Output:

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

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

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

Method #1: Using For loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to the number of 1 to parent loop iterator value using another For loop(Nested For Loop).
  • Print the value of the product of parent loop iterator value and inner loop iterator value with space.
  • After the end of the inner For loop print the Newline Character.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

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

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

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

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

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

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

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

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

Output:

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

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows as user input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to the number of 1 to parent loop iterator value using another For loop(Nested For Loop).
  • Print the value of the product of the parent loop iterator value and inner loop iterator value with space.
  • After the end of the inner For loop print the Newline Character.
  • The Exit of the Program.

1) Python Implementation

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

Below is the implementation:

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

Output:

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

2) C++ Implementation

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

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows as user input using cin and
    // store it in a variable.
    int numberows;
    cin >> numberows;

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

    return 0;
}

Output:

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

3) C Implementation

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

Below is the implementation

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

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

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

Output:

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

Related Programs:

Python Program to Print Pattern to Display Letters of the Word

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

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

Examples:

Example1:

Input:

Given string ="BTechGeeks"

Output:

B
BT
BTe
BTec
BTech
BTechG
BTechGe
BTechGee
BTechGeek
BTechGeeks

Example2:

Input:

Given string ="Aplustopper"

Output:

A
Ap
Apl
Aplu
Aplus
Aplust
Aplusto
Aplustop
Aplustopp
Aplustoppe
Aplustopper

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

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

Method #1: Using For loop (Static Input)

Approach:

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

1) Python Implementation

Below is the implementation:

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

Output:

B
BT
BTe
BTec
BTech
BTechG
BTechGe
BTechGee
BTechGeek
BTechGeeks

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

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

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

Output:

B
BT
BTe
BTec
BTech
BTechG
BTechGe
BTechGee
BTechGeek
BTechGeeks

3) C Implementation

Below is the implementation:

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

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

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

Output:

B
BT
BTe
BTec
BTech
BTechG
BTechGe
BTechGee
BTechGeek
BTechGeeks

Method #2: Using For loop (User Input)

Approach:

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

1) Python Implementation

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

Below is the implementation:

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

Output:

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

2) C++ Implementation

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

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

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

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

Output:

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

3) C Implementation

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

Below is the implementation

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

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

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

Output:

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

Related Programs:

Python Program to Print a Unique Pyramid Pattern of Digits

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

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

Examples:

Example1:

Input:

Given number of rows = 8

Output:

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

Example2:

Input:

Given number of rows = 7

Output:

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

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

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

Method #1: Using For Loop (Static Input)

Approach:

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

1) Python Implementation

Below is the implementation:

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

Output:

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

2) C++ Implementation

Below is the implementation:

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

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

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

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

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

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

Output:

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

Method #2: Using For Loop (User Input)

Approach:

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

1) Python Implementation

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

Below is the implementation:

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

Output:

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

2) C++ Implementation

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

Below is the implementation:

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

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

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

    return 0;
}

Output:

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

3) C Implementation

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

Below is the implementation:

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

    // Give the number of rows as user input using scanf and
    // store it in a variable.
    int numberOfRows;
    scanf("%d", &numberOfRows);
    // Loop from 1 to the number of rows using For loop.

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

Output:

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

Related Programs: