Reverse Digits of an integer – Python Program – One of Most Common Interview Question

Python program to reverse a number, reverse digits of a number, reverse integer, python programs, python programs to reverse a number, basic python programs.

Reverse Digits of an integer –  One of Most Common Interview Question

Problem 

– Write a Python program to reverse digits of an integer.

Example 1 :

Input : 123,

Output : 321

Example 2 :

Input : -234

Output : -432

Solution

class Solution(object):
    def reverse_solution1(self, x):
        y=int(str(abs(x))[::-1])
        if x<0: y=-1*y
        return 0 if y> (2**31)-1 or y< -1*(2**31) else y
    
    def reverse_solution2(self, x):   
        num = 0
        while ( x > abs(0) ) :
            d = x%10
            num = num*10 + d
            x = x/10
            
        return num

 

Tags: Python program to reverse a number, reverse digits of a number, reverse integer, python programs, python programs to reverse a number, basic python programs

Leave a Comment

%d bloggers like this:
Python4U
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.