You are looking for a
Palindromic number. The code you provided shows a number of shortcomings, but for now I will focus on those that are required to get you the correct results.
1. In
main()
you defined a variable
j
, but then you introduced another variable
j
in your first
for
loop. Please note that the latter variable will hide the current value of the previously declared variable. Also, depending on compiler, the later defined variable may or may not continue to exist past the end of the
for
loop. So it is unclear what you are referring to in the last few lines of your code.
Solution: the last 4 lines before the
return
statement probably should be moved inside the
for
loop; you will not need to declare the variable
j
at the start of
main
then. Alternately, remove the declaration and initialization of
j
from the
for
loop header.
2. In
a
you compute the number of digits of the number you last read. But not quite: since your
while
loop tests ahead, the resulting number will be 1 digit short. This may or may not have been your intention.
Solution: just test
while(temp>0)
rather than
while (temp/10>0)
3. Your digit counter
a
is never re-initialized after reading the next number!
Solution: move the initialization of
a
to immediately before the
while
loop.
4. Your nested
for
loop tries to do two things at the same time: extract the digits of your original number and calculate its reverse. This is a bad idea for at least two reasons: first it's much harder to find out which part of the loop is buggy, second you cannot calculate the reverse of your original number in the same order that you use to decompose it into digits - you have to reverse that order first!
Solution: create an array to hold the digits on the heap (you already calculated the number of digits in
a
, so you know the array size you need). Then extract the code for reading the digits from your number into one function. Then write a function to calculate a number from an array of decimal digits. Either make that second function read the digits from the back of the array, or use another function to first reverse its order.
5. Of course, you could avoid much of this work, if you didn't bother with numeric values at all - all you have to do is read the input as a string and reverse its characters...