{"id":20066,"date":"2021-09-06T13:34:28","date_gmt":"2021-09-06T08:04:28","guid":{"rendered":"https:\/\/python-programs.com\/?p=20066"},"modified":"2021-11-22T18:36:28","modified_gmt":"2021-11-22T13:06:28","slug":"python-program-to-find-the-sum-of-series-9-99-999-n","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-sum-of-series-9-99-999-n\/","title":{"rendered":"Python Program to Find the Sum of Series 9+99+999…..+N"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Remove the last Word from the String\/Sentence<\/a><\/p>\n Given a number N and the task is to find the sum of the given series (9+99+999…..+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 (9+99+999…..+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 The best way to learn Python for Beginners is to practice as much as they can taking help of the Sample Python Programs For Beginners<\/a>. Using them you can develop code on your own and master coding skills.<\/p>\n In the previous article, we have discussed Python Program to Remove the last Word from the String\/Sentence Given a number N and the task is to find the sum of the given series (9+99+999…..+N)\u00a0for the given number in Python. Examples: Example1: Input: Given Number =6 Output: The total sum of the series till the given …<\/p>\nGiven Number =6<\/pre>\n
The total sum of the series till the given number { 6 } = 1111104<\/pre>\n
Given Number =11<\/pre>\n
The total sum of the series till the given number { 11 } = 111111111099<\/pre>\n
Program to Find the Sum of Series 9+99+999…..+N in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the number N as static input and store it in a variable.\r\ngvn_numb = 6\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 9.\r\nk = 9\r\n# Iterate from 1 to the given number using the for loop.\r\nfor itr in range(1, gvn_numb+1):\r\n # Inside the loop, add the value of k to the above-initialized resltsum and store\r\n # it in the same variable resltsum.\r\n resltsum += k\r\n # Calculate the value of (k*10)+9 and store it in the same variable k.\r\n k = (k*10)+9\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 { 6 } = 1111104<\/pre>\n
Method #2: Using For 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 9.\r\nk = 9\r\n# Iterate from 1 to the given number using the for loop.\r\nfor itr in range(1, gvn_numb+1):\r\n # Inside the loop, add the value of k to the above-initialized resltsum and store\r\n # it in the same variable resltsum.\r\n resltsum += k\r\n # Calculate the value of (k*10)+9 and store it in the same variable k.\r\n k = (k*10)+9\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 = 11\r\nThe total sum of the series till the given number { 11 } = 111111111099<\/pre>\n
\n