- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Advertisements