Problem of reverse printing

i wrote the code for this question and after submitting it i got full marks and it also got accepted with a green tick.

but when i try to run the same code at my windows powershell then it sometimes displays wrong output.

for example when i input 123 then it gives output as 320

@pushkar24sharma please save your code on ide.codingblocks.com and share the link, I’ll have a look at it.


@Ishitagambhir maam this is the link

hi @pushkar24sharma i ran this code on my local machine for multiple cases and it is giving correct answer. All your variables are initialised properly, so the issue of previous garbage memory should not be there.
Can you share a screenshot of any instance where it is giving wrong answer?
Make sure that the number, and its reverse are both in the range of int else it might overflow and give incorrect answer. I am attaching a screenshot of all the cases I tested the code for.

image

@Ishitagambhir maam this is the screenshot -


see when i input 123 then it gives 320 as output
when i input 123456789 it gives 987654319 as output

but when i run thi code on coding blocks ide it gives correct answers also it was accepted with 100 points when i submitted this code.

why this is happening??
is there any problem with my windows powershell??

@Ishitagambhir maam also when i try to run this code by using my command prompt it gives the same incorrect answer.
please see this screenshot of command prompt -

@pushkar24sharma
There is nothing wrong with the code as far as I can tell, and I also ran on powershell only, so it doesn’t seem like it is a logical error. (it wouldn’t give different outputs if there was a logical error)
Initialize all the variables with 0 or any other suitable value and then try running the code. If it is still giving incorrect output, print the value of rev, answer, etc in the loops and share the new code and output of screenshot so I can get a better idea of why it is faltering for the last few digits.

@Ishitagambhir maam even after initialising every variable with 0, it is not working.

i also modified the code by printing the every variable inside the loop. the first while loop is giving correct answer, but the second while loop is creating problem. it is displaying the wrong value of reverse variable every time.

here is the new modified code -

@Ishitagambhir maam the screenshot of the output is here -

@pushkar24sharma
try changing this line here. It is clear that a 1 is being subtracted from reverse, im not sure where. There are brackets so precedence should not change. Still try this once and let me know if there is any change in the output. You may try changing the value of dig in some other line too.
image

@Ishitagambhir maam it is still not working. i did what u said.

also i tried this one -
reverse=reverse+(r*pow(10,(dig-1)));
–dig;
on the place of previous statement.

it is giving the same result.

and why is it showing incorrect answer? when i submitted this same code it was accepted there and was also showing correct output there. i tried to run the same code on coding blocks ide site, it was giving me correct answer, but it gives wrong answer only on my windows powershell.

@pushkar24sharma it seems to be an issue specific to your machine, because I compiled on powershell using the g++ compiler. And as you said it passed all the test cases also.

1 Like

okay thank you @Ishitagambhir maam

1 Like

hi @pushkar24sharma please mark your doubt as resolved

1 Like