Python Program to concatenate numbers and calculate n+nn+nnn

March 9, 2022, Learn eTutorial

In this simple python program, we need to concatenate the numbers and calculate the sum. It is a beginner-level python program.

To understand this example, you should have knowledge of the following Python programming topics:

How to concatenate numbers and calculate the sum in python?

In this number python program example, we need to find the sum of concatenating of a number n using a pattern of n + nn + nnn. For example, let us take a number 5, and we have to use the pattern 5 + 55 + 555 = 615.

To solve the python problem, we first need to change the number 'n' to a string. Then we concatenate the string 'n' to 'nn' and 'nnn' then convert back the string to a number and calculate the sum of the concatenated numbers and  print the result .

We use int and str functions of python language to convert the string to integer and then integer to string. Use a temp variable to store the string number, and then we concatenate the temp + temp to another variable. And temp + temp + temp  to another variable. Finally, we calculate the sum by converting the string to an integer using int.


STEP 1: Enter the number using the input method and convert that input string to an integer using int() in python programming.

STEP 2: Convert the integer to a string and store that value in a variable for concatenate.

STEP 3: Use two variables to store the values after concatenating the string using temp + temp and temp + temp + temp.

STEP 4: Now we have the strings, and convert the string to number using int() and calculate the sum using python language.

STEP 5: Print the value of the result using a print statement in python basics.

Python Source Code

                                          n=int(input("Enter a number n: "))


t1=temp+temp     # concatenate the strings


comp=n+int(t1)+int(t2)    # calculate the sum 

print("The sum is:",comp)


Enter the Number n :  5

The sum is : 615