{"id":20074,"date":"2021-09-06T13:36:34","date_gmt":"2021-09-06T08:06:34","guid":{"rendered":"https:\/\/python-programs.com\/?p=20074"},"modified":"2021-11-22T18:36:28","modified_gmt":"2021-11-22T13:06:28","slug":"python-program-to-print-triangular-number-series-1-3-6-10-15-n","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-triangular-number-series-1-3-6-10-15-n\/","title":{"rendered":"Python Program to Print Triangular Number series 1 3 6 10 15 …N"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Sum of Series 1-2+3-4+5…+N<\/a><\/p>\n Given a number N and the task is to find the triangular number series (1 3 6 10 15 …N)<\/strong> until the given number.<\/p>\n int() function in Python:<\/strong><\/p>\n The int() function is used to transform a value into an integer number.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the triangular number series (1 3 6 10 15 …N)<\/strong> until the given number N :<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n If you wanna write simple python programs as a part of your coding practice refer to numerous Simple Python Program Examples<\/a> existing and learn the approach used.<\/p>\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 …<\/p>\nGiven Number = 8<\/pre>\n
The Triangular series until the given number{ 8 } is :\r\n1 3 6 10 15 21 28 36<\/pre>\n
Given Number = 15<\/pre>\n
The Triangular series until the given number{ 15 } is :\r\n1 3 6 10 15 21 28 36 45 55 66 78 91 105 120<\/pre>\n
Program to Print Triangular Number series 1 3 6 10 15 …N in Python<\/h2>\n
\n
Method #1: Using While Loop (Static Input)<\/h3>\n
\n
# Give the number N as static input and store it in a variable.\r\ngvn_numb = 8\r\n# Take a variable say k and initialize its value with 1.\r\nk = 1\r\nprint(\"The Triangular series until the given number{\", gvn_numb, \"} is :\")\r\n# Loop until the value of k is less than or equal to the given number using the while loop.\r\nwhile(k <= gvn_numb):\r\n # Inside the loop, multiply the value of k with k+1 and divide the result by 2 and\r\n # convert it into an integer using the int() function.\r\n # Store it in a variable say p.\r\n p = int((k*(k+1))\/2)\r\n # Print the above result (p) separated by spaces to get the triangular series until\r\n # the given number.\r\n print(p, end=\" \")\r\n # Increment the value of k by 1 and store it in the same variable k.\r\n k += 1\r\n<\/pre>\n
The Triangular series until the given number{ 8 } is :\r\n1 3 6 10 15 21 28 36<\/pre>\n
Method #2: Using While loop (User Input)<\/h3>\n
\n
# Give the number N as user input using the int(input()) function and\r\n# store it in a variable.\r\ngvn_numb = int(input(\"Enter some Random Number = \"))\r\n# Take a variable say k and initialize its value with 1.\r\nk = 1\r\nprint(\"The Triangular series until the given number{\", gvn_numb, \"} is :\")\r\n# Loop until the value of k is less than or equal to the given number using the while loop.\r\nwhile(k <= gvn_numb):\r\n # Inside the loop, multiply the value of k with k+1 and divide the result by 2 and\r\n # convert it into an integer using the int() function.\r\n # Store it in a variable say p.\r\n p = int((k*(k+1))\/2)\r\n # Print the above result (p) separated by spaces to get the triangular series until\r\n # the given number.\r\n print(p, end=\" \")\r\n # Increment the value of k by 1 and store it in the same variable k.\r\n k += 1\r\n<\/pre>\n
Enter some Random Number = 15\r\nThe Triangular series until the given number{ 15 } is :\r\n1 3 6 10 15 21 28 36 45 55 66 78 91 105 120<\/pre>\n
\n