Program to Print Triangular Number series 1 3 6 10 15 ...N

Python Program to Print Triangular Number series 1 3 6 10 15 …N

In the previous article, we have discussed Python Program to Find Sum of Series 1-2+3-4+5…+N

Given a number N and the task is to find the triangular number series (1 3 6 10 15 …N) until the given number.

int() function in Python:

The int() function is used to transform a value into an integer number.

Examples:

Example1:

Input:

Given Number = 8

Output:

The Triangular series until the given number{ 8 } is :
1 3 6 10 15 21 28 36

Example2:

Input:

Given Number = 15

Output:

The Triangular series until the given number{ 15 } is :
1 3 6 10 15 21 28 36 45 55 66 78 91 105 120

Program to Print Triangular Number series 1 3 6 10 15 …N in Python

Below are the ways to find the triangular number series (1 3 6 10 15 …N) until the given number N :

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Take a variable say k and initialize its value with 1.
  • Loop until the value of k is less than or equal to the given number using the while loop.
  • Inside the loop, multiply the value of k with k+1 and divide the result by 2 and convert it into an integer using the int() function.
  • Store it in a variable say p.
  • Print the above result (p) separated by spaces to get the triangular series until the given number.
  • Increment the value of k by 1 and store it in the same variable k.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvn_numb = 8
# Take a variable say k and initialize its value with 1.
k = 1
print("The Triangular series until the given number{", gvn_numb, "} is :")
# Loop until the value of k is less than or equal to the given number using the while loop.
while(k <= gvn_numb):
   # Inside the loop, multiply the value of k with k+1 and divide the result by 2 and
   # convert it into an integer using the int() function.
   # Store it in a variable say p.
    p = int((k*(k+1))/2)
   # Print the above result (p) separated by spaces to get the triangular series until
   # the given number.
    print(p, end=" ")
   # Increment the value of k by 1 and store it in the same variable k.
    k += 1

Output:

The Triangular series until the given number{ 8 } is :
1 3 6 10 15 21 28 36

Method #2: Using While loop (User Input)

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Take a variable say k and initialize its value with 1.
  • Loop until the value of k is less than or equal to the given number using the while loop.
  • Inside the loop, multiply the value of k with k+1 and divide the result by 2 and convert it into an integer using the int() function.
  • Store it in a variable say p.
  • Print the above result (p) separated by spaces to get the triangular series until the given number.
  • Increment the value of k by 1 and store it in the same variable k.
  • The Exit of the Program.

Below is the implementation:

# Give the number N 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 say k and initialize its value with 1.
k = 1
print("The Triangular series until the given number{", gvn_numb, "} is :")
# Loop until the value of k is less than or equal to the given number using the while loop.
while(k <= gvn_numb):
   # Inside the loop, multiply the value of k with k+1 and divide the result by 2 and
   # convert it into an integer using the int() function.
   # Store it in a variable say p.
    p = int((k*(k+1))/2)
   # Print the above result (p) separated by spaces to get the triangular series until
   # the given number.
    print(p, end=" ")
   # Increment the value of k by 1 and store it in the same variable k.
    k += 1

Output:

Enter some Random Number = 15
The Triangular series until the given number{ 15 } is :
1 3 6 10 15 21 28 36 45 55 66 78 91 105 120

If you wanna write simple python programs as a part of your coding practice refer to numerous Simple Python Program Examples existing and learn the approach used.