Sunday, September 28, 2025
HomeLanguagesCalculate n + nn + nnn + … + n(m times) in...

Calculate n + nn + nnn + … + n(m times) in Python

The program is to find a mathematical series, where we need to accept the value of n and m. n is the base number and m is the number of times till which the series run.

Examples:

Input : 2 + 22 + 222 + 2222 + 22222 
Output : 24690

Input : 12 + 1212 + 121212
Output : 122436

We first convert the numbers into string format and concatenate them regularly. Later, we convert them back to integer and add them upto mth term. as shown in the following program.




# Python program to sum the given series
  
# Returns sum of n + nn + nnn + .... (m times)
def Series(n, m):
  
    # Converting the number to string
    str_n = str(n)
  
    # Initializing result as number and string
    sums = n
    sum_str = str(n)
  
    # Adding remaining terms
    for i in range(1, m):
         
        # Concatenating the string making n, nn, nnn...
        sum_str = sum_str + str_n
          
        # Before adding converting back to integer
        sums = sums + int(sum_str)
  
    return sums
  
# Driver Code
n = 2
m = 5
total = Series(n, m)
print(total)


Output:

24690
RELATED ARTICLES

Most Popular

Dominic
32324 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6695 POSTS0 COMMENTS
Nicole Veronica
11860 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11918 POSTS0 COMMENTS
Shaida Kate Naidoo
6807 POSTS0 COMMENTS
Ted Musemwa
7073 POSTS0 COMMENTS
Thapelo Manthata
6763 POSTS0 COMMENTS
Umr Jansen
6771 POSTS0 COMMENTS