
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Calculate n + nn + nnn + u + n(m times) in Python Program
In this tutorial, we are going to write code to find the sum of the series n + nn + nnn + ... + n (m times). We can do it very easily in Python. Let's see some examples.
Input: n = 1 m = 5 Series: 1 + 11 + 111 + 1111 + 11111 Output: 12345
Algorithm
Follow the below steps to solve the problem.
1. Initialise the n and m. 2. Initialise total to 0. 3. Make the copy of n to generate next number in the series. 4. Iterate the loop m times. 4.1. Add n to the total. 4.2. Update the n with n * 10 + copy_n. 5. Print the total.
Example
See the code below.
# initializing n and m n = 1 m = 5 # initializing total to 0 total = 0 # making the copy of n to get next number in the series copy_n = n # iterating through the loop for i in range(m): # adding n to total total += n # updating n to get next number in the serias n = n * 10 + copy_n # printing the total print(total)
Output
If you run the above code, you will get the following results.
12345
Conclusion
If you have any doubts in the article, mention them in the comment section.
- Related Articles
- Calculate n + nn + nnn + … + n(m times) in Python program
- Calculate n + nn + nnn + ? + n(m times) in Python
- Plus One in Python
- Cplus plus vs Java vs Python?
- LocalDateTime plus() method in Java
- Duration plus() method in Java
- LocalTime plus() method in Java
- Instant plus() method in Java
- LocalDate plus() method in Java
- C Program to check Plus Perfect Number
- Plus One Linked List in C++
- Finding all possible combined (plus and minus) sums of n arguments JavaScript
- What is Unary Plus Operator in JavaScript?
- Now call facebook stream to google plus
- What is the Cost plus pricing method?

Advertisements