What happens when last digit takens as zero Ex:- if i want to reverse a num 7890 What will the output I run the program of 7890 I got the output as 987 but it doesn't show 0 at starting in reverse of a number. Could pls explain this.
How is the integer stored in this case? As a string? If so we could just reverse the string. If it's not stored as a string, then how do we know how many leading zeros there are?
@@mxhell3867 Ah OK, well if it's a string, and it's a positive integer for sure, then you could technically just reverse the string itself: ua-cam.com/video/PtSHcou0WIs/v-deo.html. 🙂 If it could be a negative number, then you could check if the first char in the string is '-', and if it is, reverse the portion of the string after the '-' only.
@@PortfolioCourses Try it, it will throw the code into an infinite loop because negative numbers don't return 0 after dividing it again and again. Only 1 minus(-) can change the whole game. Maybe thats why my maths teacher told me to be careful if the problem contains minus 😆
@@lumiplex404 No that's incorrect, it works. Did you try it? I tried using negative numbers before I created the video, and then again after your comment, and it works. I've now tried it for a 3rd time on 3 different compilers, and it still works. 🙂
@@lumiplex404 I don't understand what leetcode has to do with this solution being correct or incorrect with negative integers. Did you try it yet? The code is available here: github.com/portfoliocourses/c-example-code/blob/main/reverse_integer.c
SUCH A LIFE SAVER THANK YOU FOR THIS!
You're very welcome! 😀
Well explanation, thank you
You’re welcome Tahmid! :-)
What happens when last digit takens as zero
Ex:- if i want to reverse a num 7890
What will the output
I run the program of 7890
I got the output as 987 but it doesn't show 0 at starting in reverse of a number. Could pls explain this.
Is there any way to do this without using a loop?
what will this program do at 1111111113, as it's reversal will be out of int -datatype value limits.
you can use long data type
Which code editor is it?
In this video I am using Xcode on a Mac. :-)
how to prevent signed integer overflow
You might find this stackoverflow answer helpful: stackoverflow.com/a/62943541. :-)
thanks!
Thanks 😅
How to reverse an integer with leading zeroes or only zeroes?
Like :
Input: 009921
Ouput: 129900
Input: 000
Output: 000
Thanks in advance!
How is the integer stored in this case? As a string? If so we could just reverse the string. If it's not stored as a string, then how do we know how many leading zeros there are?
the leading zeros don't have a value so thee programme wouldn't count them or know how many zeros were used there.
@@PortfolioCourses sorry for late reply, yeah we have to work with strings otherwise we won't even know if user put any zeroes..
@@mxhell3867 Ah OK, well if it's a string, and it's a positive integer for sure, then you could technically just reverse the string itself: ua-cam.com/video/PtSHcou0WIs/v-deo.html. 🙂 If it could be a negative number, then you could check if the first char in the string is '-', and if it is, reverse the portion of the string after the '-' only.
Try reversing a negative number with this programme :)
It works with negative numbers. :-)
@@PortfolioCourses Try it, it will throw the code into an infinite loop because negative numbers don't return 0 after dividing it again and again. Only 1 minus(-) can change the whole game. Maybe thats why my maths teacher told me to be careful if the problem contains minus 😆
@@lumiplex404 No that's incorrect, it works. Did you try it? I tried using negative numbers before I created the video, and then again after your comment, and it works. I've now tried it for a 3rd time on 3 different compilers, and it still works. 🙂
@@PortfolioCourses Even in the leetcode solutions, they avoided negative numbers in the following way
if x < 0:
return False
@@lumiplex404 I don't understand what leetcode has to do with this solution being correct or incorrect with negative integers. Did you try it yet? The code is available here: github.com/portfoliocourses/c-example-code/blob/main/reverse_integer.c