{"id":19925,"date":"2021-09-02T15:27:30","date_gmt":"2021-09-02T09:57:30","guid":{"rendered":"https:\/\/python-programs.com\/?p=19925"},"modified":"2021-11-22T18:36:29","modified_gmt":"2021-11-22T13:06:29","slug":"python-program-to-find-sum-of-the-series-1-1-2-1-2-3-till-n","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-sum-of-the-series-1-1-2-1-2-3-till-n\/","title":{"rendered":"Python Program to Find Sum of Series (1+(1+2)+(1+2+3)+…till N)"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Swap Upper Diagonal Elements with Lower Diagonal Elements of Matrix.<\/a><\/p>\n Given a number N and the task is to find the sum of the given series (1+(1+2)+(1+2+3)+…till N)<\/strong>\u00a0for the given number in Python.<\/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 sum of the given series (1+(1+2)+(1+2+3)+…till N)<\/strong> for the given number in Python<\/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 Find a comprehensive collection of Examples of Python Programs<\/a> ranging from simple ones to complex ones to guide you throughout your coding journey.<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In the previous article, we have discussed Python Program to Swap Upper Diagonal Elements with Lower Diagonal Elements of Matrix. Given a number N and the task is to find the sum of the given series (1+(1+2)+(1+2+3)+…till N)\u00a0for the given number in Python. Examples: Example1: Input: Given Number = 25 Output: The total sum of …<\/p>\nGiven Number = 25<\/pre>\n
The total sum of the series till the given number { 25 } = 2925<\/pre>\n
Given Number = 18<\/pre>\n
The total sum of the series till the given number { 18 } = 1140<\/pre>\n
Program to Find Sum of Series (1+(1+2)+(1+2+3)+…till 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 = 25\r\n# Take a variable say resltsum which gives the sum of the given series till N and\r\n# initialize its value to 0.\r\nresltsum = 0\r\n# Take another variable say k and initialize its value with 1.\r\nk = 1\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, iterate from 1 to the value of k using the for loop.\r\n for itr in range(1, k+1):\r\n # Add the iterator value to the above-initialized resltsum and store it in the same\r\n # variable resltsum.\r\n resltsum += itr\r\n # Increment the value of k by 1 outside the for loop.\r\n k += 1\r\n# Print the resltsum value which is the result of the series till the given Number N.\r\nprint(\r\n \"The total sum of the series till the given number {\", gvn_numb, \"} = \", resltsum)\r\n<\/pre>\n
The total sum of the series till the given number { 25 } = 2925<\/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 resltsum which gives the sum of the given series till N and\r\n# initialize its value to 0.\r\nresltsum = 0\r\n# Take another variable say k and initialize its value with 1.\r\nk = 1\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, iterate from 1 to the value of k using the for loop.\r\n for itr in range(1, k+1):\r\n # Add the iterator value to the above-initialized resltsum and store it in the same\r\n # variable resltsum.\r\n resltsum += itr\r\n # Increment the value of k by 1 outside the for loop.\r\n k += 1\r\n# Print the resltsum value which is the result of the series till the given Number N.\r\nprint(\r\n \"The total sum of the series till the given number {\", gvn_numb, \"} = \", resltsum)\r\n<\/pre>\n
Enter some Random Number = 18\r\nThe total sum of the series till the given number { 18 } = 1140<\/pre>\n
\n