In a palindrome program why we store number in another variable and why we declare rev=0

–1 vote
asked Jul 7, 2019 by VAIBHAV (110 points)
n=num;

while(num!=0)

{

  d=num%10;

}

rev=rev*10+d;

num=num/10;

2 Answers

+1 vote
answered Jul 12, 2019 by Loki (1,600 points)

n=num;

while(num!=0)

  d=num%10;
  rev=rev*10+d;
  num=num/10;

}

First the above should be the correct way of writing this, last two statements must be included in the while loop considering 'rev' and 'd' are declared, now we store the number 'num' in another variable in this program as for controlling this while loop we constantly change the value of 'num' till it has 0 inside it(i.e., till we get all the numbers out in 'd' one by one and given to 'rev' in reverse order) So we must store it somewhere else if we don't want to lose it. Also we should assign rev = 0 because if we only declare it as (int rev) then it might have some garbage value and show us incorrect result.

0 votes
answered Jul 18, 2019 by Randhir Gurjar
we use rev=0, as it  might contain garbage value.

we store value in another variable to control the loop so it can check all the possible values.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...