Basic-Python-Programs icon indicating copy to clipboard operation
Basic-Python-Programs copied to clipboard

Reverse of a user input number with best time complexity

Open KrishPanchal17 opened this issue 2 years ago • 0 comments

I have changed the code as it was the code in which user input option was not available, so the user cannot input the number He/She wants which makes the code less efficient and also time complexity of that code was high. So I changed few things in the code, my code defines a function reverse_number that takes an integer n and returns its reverse by repeatedly extracting the last digit and adding it to the reversed number. This approach has a time complexity of O(log10(n)), which is considered to be the best time complexity for reversing a number as it processes each digit only once. The program also handles invalid input by catching the ValueError exception in case the user enters a non-integer value.

KrishPanchal17 avatar Oct 09 '23 12:10 KrishPanchal17