Python program to switch a given number (2 distinct ways)

Switching a number in Python: Here, we will learn various approaches to turn around a given number in Python.

Example:

    Input:
    12345

    Output:
    54321

Here, we are actualizing program to turning around a given number utilizing 2 distinct ways.

1) Famous methodology for turning around the number: Take contribution from the client and pigeonhole into a whole number, at that point emphasizes on the up and up till num isn’t gotten zero, inside the circle:

  • Discover the rest of.
  • Utilizing this: rev_num = rev_num * 10 + leftover portion.
  • Update that number by plunging by 10.
  • In the wake of leaving the circle printing the switch number.
if __name__ == "__main__" :

    # take string input from user
    num = int(input('Enter a number: '))

    rev_num = 0

    # iterate the loop till num is not equal to zero
    while(num) :
        rem = num % 10
        rev_num = rev_num* 10 + rem
        num //= 10
    
    print('Reverse number is: ', rev_num)

Output:

Enter a number: 12345
Reverse number is:  54321

2) Make a client characterized work for turning around the Number: Take contribution from the client and pigeonhole into a whole number, thenreverseNum() work call.

  • Inside the capacity:
  • Emphasize on top of it till num doesn’t get zero:
  • Discover the rest of.
  • Utilizing this: rev_num = rev_num * 10 + leftover portion.
  • Update that number by jumping by 10.
  • In the wake of leaving the circle restoring the turn around the number to the primary.
# define a function for finding 
# reverse of the number
def reverseNum(num) :
    
    rev_num = 0

    # iterate the loop till num is not equal to zero
    while(num) :
        rem = num % 10
        rev_num = rev_num* 10 + rem
        num //= 10

    return rev_num


# Main() method
if __name__ == "__main__" :

    # take string input from user
    num = int(input('Enter a number: '))
    
    print('Reverse number is: ', reverseNum(num))

Output:

Enter a number: 12345
Reverse number is:  54321

Leave a Comment

error: Alert: Content is protected!!