Program to Find the Sum of Series 9+99+999.....+N

Python Program to Find the Sum of Series 9+99+999…..+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) for the given number in Python.

Examples:

Example1:

Input:

Given Number =6

Output:

The total sum of the series till the given number { 6 } =  1111104

Example2:

Input:

Given Number =11

Output:

The total sum of the series till the given number { 11 } =  111111111099

Program to Find the Sum of Series 9+99+999…..+N in Python

Below are the ways to find the sum of the given series (9+99+999…..+N) for the given number in Python

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Take another variable say k and initialize its value with 9.
  • Iterate from 1 to the given number using the for loop.
  • Inside the loop, add the value of k to the above-initialized resltsum and store it in the same variable resltsum.
  • Calculate the value of (k*10)+9 and store it in the same variable k.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvn_numb = 6
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 0.
resltsum = 0
# Take another variable say k and initialize its value with 9.
k = 9
# Iterate from 1 to the given number using the for loop.
for itr in range(1, gvn_numb+1):
  # Inside the loop, add the value of k to the above-initialized resltsum and store
  # it in the same variable resltsum.
    resltsum += k
  # Calculate the value of (k*10)+9 and store it in the same variable k.
    k = (k*10)+9
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

The total sum of the series till the given number { 6 } =  1111104

Method #2: Using For 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 resltsum which gives the sum of the given series till N and initialize its value to 0.
  • Take another variable say k and initialize its value with 9.
  • Iterate from 1 to the given number using the for loop.
  • Inside the loop, add the value of k to the above-initialized resltsum and store it in the same variable resltsum.
  • Calculate the value of (k*10)+9 and store it in the same variable k.
  • Print the resltsum value which is the result of the series till the given Number N.
  • 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 resltsum which gives the sum of the given series till N and
# initialize its value to 0.
resltsum = 0
# Take another variable say k and initialize its value with 9.
k = 9
# Iterate from 1 to the given number using the for loop.
for itr in range(1, gvn_numb+1):
  # Inside the loop, add the value of k to the above-initialized resltsum and store
  # it in the same variable resltsum.
    resltsum += k
  # Calculate the value of (k*10)+9 and store it in the same variable k.
    k = (k*10)+9
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number {", gvn_numb, "} = ", resltsum)

Output:

Enter some random Number = 11
The total sum of the series till the given number { 11 } = 111111111099

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. Using them you can develop code on your own and master coding skills.