Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Write a program that gets an integer , which its number of digits is not specified. then print each digit n times if n is the digit itself

How can I solve this problem?
like this:
6041
6: 666666
0:
4: 4444
1: 1
I just know that for separating numbers we should do this, I think:

num = int(input)
while num > 0:
res = num // 10
num = num % 10

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

One way to do it is to keep the number as a string, then in a loop convert the first digit to an int, print it n times, then remove it. Repeat the loop until the string is empty:

num = input("Input a number: ")
while len(num) > 0:
    digit = num[0]
    for i in range(int(digit)):
        print(digit, end="")
    num = num[1:]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading