-4

Guys I got one doubt in palindrome. I write one program for Number palindrome. It executes Successfully, But when I enter 333 I got the correct output, When I put 111 I got the wrong output. I don't know what is wrong, Because when I execute above 3 that will give correct output, But when I give below 3 I got the wrong output. Plz, help me to What I did a mistake.

public class Practice1 {
    public static void main(String[] args) {
        int num = 121, res = 0, var = 0, c = num;
        for (int i = 1; i <= num; i++) {
            res = num % 10;
            var = var * 10 + res;
            num = (num) / 10;
        }
        if (var == c) {
            System.out.println(c + " is a Palindrome Number");
        } else {
            System.out.println(c + " is not a Palindrome Number");
        }
    } 
}
ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • 1
    `for(int i=1;i<=num;i++)` looks really weird seeing what you're doing with `num`. – Federico klez Culloca May 02 '20 at 20:04
  • 2
    Does this answer your question? [How to check if a number is palindrome in Java?](https://stackoverflow.com/questions/43516020/how-to-check-if-a-number-is-palindrome-in-java) – Harshal Parekh May 02 '20 at 20:05
  • Pro-tip: questions featuring the word "plz" are liable to be downvoted, and if it appears in the title, they'll get a couple more. Questions are here for the benefit of future readers - so it is ideal if they can be made as readable as possible. Technical writing is preferred. – halfer May 09 '20 at 12:39

1 Answers1

0

You can use while loop

    public static void main(String[] args) {
        int num = 121, res = 0, var = 0, c = num;
        while (num != 0) {
            res = num % 10;
            var = var * 10 + res;
            num = (num) / 10;
        }
        if (var == c) {
            System.out.println(c + " is a Palindrome Number");
        } else {
            System.out.println(c + " is not a Palindrome Number");
        }
    }
0xh3xa
  • 4,801
  • 2
  • 14
  • 28
  • 1
    Please flag the question as a [duplicate](https://stackoverflow.com/questions/43516020/how-to-check-if-a-number-is-palindrome-in-java) and move on instead of reiterating obvious answers. – Harshal Parekh May 02 '20 at 20:11
  • 1
    I have marked as duplicated – 0xh3xa May 02 '20 at 20:13