Reverse of a number with leading zeroes

后端 未结 10 1542
庸人自扰
庸人自扰 2021-01-14 05:17

How do we reverse a number with leading zeroes in number ? For ex: If input is 004, output should be 400.

I wrote below program but it works only when n

相关标签:
10条回答
  • 2021-01-14 06:02
    s = int(raw_input(" enter the no of tyms :"))
    n = 0
    list, list1 = [], []
    
    while n <= s:
        m = raw_input("enter the number:")
            n=n+1
            list.append(m)
    
    print list
    list.reverse()
    print list
    

    Reverse in one of the best lang Python.

    0 讨论(0)
  • 2021-01-14 06:06

    Replace your while loop with a for loop with the same number of runs as you wish the original number has digits (including leading zeros). e.g. 004 would require the loop to be run 3 times, and not to terminate prematurely once x == 0.

    0 讨论(0)
  • 2021-01-14 06:08

    As ChrisF said, you need to load a string, because 4 and 004 is the same int and you cannot distinguish it after you assign it to an int variable.

    The next thing to do is trim the string to contain just digits (if you want to be correct) and run std::reverse on it - and you're done.

    0 讨论(0)
  • 2021-01-14 06:09

    Leading zeroes are not represented by binary numbers (int, double, etc.) So you'll probably have to use std::string. Read the input into the string, then call std::reverse() passing the string as input.

    0 讨论(0)
提交回复
热议问题